Skip to content

Commit 89c0470

Browse files
committed
Add tests and fixtures
1 parent 974805c commit 89c0470

File tree

13 files changed

+293
-14
lines changed

13 files changed

+293
-14
lines changed

.circleci/config.yml

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
version: 2
2+
3+
defaults: &defaults
4+
working_directory: ~/repo
5+
docker:
6+
- image: circleci/node
7+
8+
jobs:
9+
test:
10+
<<: *defaults
11+
steps:
12+
- checkout
13+
- restore_cache:
14+
keys:
15+
- v1-dependencies-{{ checksum "yarn.lock" }}
16+
# fallback to using the latest cache if no exact match is found
17+
- v1-dependencies-
18+
19+
- run:
20+
name: Install and build package
21+
command: yarn install --frozen-lockfile
22+
23+
- run:
24+
name: Run tests
25+
command: yarn test --runInBand --no-cache --coverage
26+
27+
- save_cache:
28+
paths:
29+
- node_modules
30+
key: v1-dependencies-{{ checksum "yarn.lock" }}
31+
32+
- persist_to_workspace:
33+
root: ~/repo
34+
paths: .
35+
36+
workflows:
37+
version: 2
38+
test-deploy:
39+
jobs:
40+
- test

package.json

+3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
},
2323
"scripts": {
2424
"build": "tsc -p tsconfig.json",
25+
"test": "jest --config ./test/jest.config.json",
2526
"prepare": "npm run build"
2627
},
2728
"files": [
@@ -71,6 +72,8 @@
7172
"husky": "^2.1.0",
7273
"lint-staged": "^8.1.0",
7374
"prettier": "^1.17.0",
75+
"ps-tree": "^1.2.0",
76+
"shelljs": "^0.8.3",
7477
"tslint": "^5.16.0",
7578
"tslint-config-palmerhq": "^1.0.2",
7679
"tslint-config-prettier": "^1.18.0",

src/createRollupConfig.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export function createRollupConfig(format, env, opts) {
3838
// Establish Rollup output
3939
output: {
4040
// Set filenames of the consumer's package
41-
file: `${paths.appDist}/${safeVariableName(
41+
file: `${paths.appDist}/${safePackageName(
4242
opts.name
4343
)}.${format}.${env}.js`,
4444
// Pass through the file format

src/index.js

+15-10
Original file line numberDiff line numberDiff line change
@@ -251,26 +251,31 @@ prog
251251
const [cjsDev, cjsProd, ...otherConfigs] = createBuildConfigs(opts);
252252
if (opts.format.includes('cjs')) {
253253
try {
254-
const promise = fs.writeFile(
255-
resolveApp('dist/index.js'),
256-
`
254+
await mkdirp(resolveApp('./dist'));
255+
const promise = fs
256+
.writeFile(
257+
resolveApp('./dist/index.js'),
258+
`
257259
'use strict'
258260
259261
if (process.env.NODE_ENV === 'production') {
260-
module.exports = require('./${safeVariableName(
262+
module.exports = require('./${safePackageName(
261263
opts.name
262264
)}.cjs.production.js')
263265
} else {
264-
module.exports = require('./${safeVariableName(
266+
module.exports = require('./${safePackageName(
265267
opts.name
266268
)}.cjs.development.js')
267269
}`,
268-
{
269-
overwrite: true,
270-
}
271-
);
270+
{
271+
overwrite: true,
272+
}
273+
)
274+
.catch(e => logError(e));
272275
logger(promise, 'Creating entry file');
273-
} catch (e) {}
276+
} catch (e) {
277+
logError(e);
278+
}
274279
}
275280
try {
276281
const promise = asyncro.map(
+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"scripts": {
3+
"build": "tsdx build"
4+
},
5+
"name": "build-default",
6+
"license": "MIT"
7+
}
+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export const sum = (a: number, b: number) => {
2+
if ('development' === process.env.NODE_ENV) {
3+
console.log('fuck');
4+
}
5+
return a + b;
6+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { sum } from '../src';
2+
3+
describe('fuck', () => {
4+
it('works', () => {
5+
expect(sum(1, 1)).toEqual(2);
6+
});
7+
});
+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"compilerOptions": {
3+
"target": "es5",
4+
"module": "ESNext",
5+
"lib": ["dom", "esnext"],
6+
"declaration": true,
7+
"sourceMap": true,
8+
"rootDir": "./",
9+
"strict": true,
10+
"noImplicitAny": true,
11+
"strictNullChecks": true,
12+
"strictFunctionTypes": true,
13+
"strictPropertyInitialization": true,
14+
"noImplicitThis": true,
15+
"alwaysStrict": true,
16+
"noUnusedLocals": true,
17+
"noUnusedParameters": true,
18+
"noImplicitReturns": true,
19+
"noFallthroughCasesInSwitch": true,
20+
"moduleResolution": "node",
21+
"baseUrl": "./",
22+
"paths": {
23+
"*": ["src/*", "node_modules/*"]
24+
},
25+
"jsx": "react",
26+
"esModuleInterop": true
27+
},
28+
"include": ["src", "types"],
29+
}

test/fixtures/util.js

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
'use strict';
2+
3+
const shell = require('shelljs');
4+
const path = require('path');
5+
const rootDir = process.cwd();
6+
7+
shell.config.silent = true;
8+
9+
module.exports = {
10+
setupStageWithFixture: (stageName, fixtureName) => {
11+
const stagePath = path.join(rootDir, stageName);
12+
shell.mkdir(stagePath);
13+
shell.exec(`cp -a ${rootDir}/test/fixtures/${fixtureName}/. ${stagePath}/`);
14+
shell.ln(
15+
'-s',
16+
path.join(rootDir, 'node_modules'),
17+
path.join(stagePath, 'node_modules')
18+
);
19+
shell.cd(stagePath);
20+
},
21+
22+
teardownStage: stageName => {
23+
shell.cd(rootDir);
24+
shell.rm('-rf', path.join(rootDir, stageName));
25+
},
26+
27+
rootDir,
28+
};

test/jest.config.json

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"roots": ["<rootDir>/tests"],
3+
"collectCoverageFrom": ["**/*.js"],
4+
"testMatch": [
5+
"<rootDir>/tests/**/?(*.)(spec|test).(ts|js)?(x)"
6+
]
7+
}

test/tests/tsdx-build.test.js

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* @jest-environment node
3+
*/
4+
'use strict';
5+
6+
const shell = require('shelljs');
7+
const util = require('../fixtures/util');
8+
9+
shell.config.silent = false;
10+
11+
const stageName = 'stage-build';
12+
13+
describe('tsdx build', () => {
14+
beforeAll(() => {
15+
util.teardownStage(stageName);
16+
});
17+
18+
it('should compile files into a dist directory', () => {
19+
util.setupStageWithFixture(stageName, 'build-default');
20+
21+
const output = shell.exec('node ../dist/index.js build');
22+
23+
expect(shell.test('-f', 'dist/index.js')).toBeTruthy();
24+
expect(
25+
shell.test('-f', 'dist/build-default.cjs.development.js')
26+
).toBeTruthy();
27+
expect(
28+
shell.test('-f', 'dist/build-default.cjs.production.js')
29+
).toBeTruthy();
30+
expect(
31+
shell.test('-f', 'dist/build-default.es.production.js')
32+
).toBeTruthy();
33+
expect(
34+
shell.test('-f', 'dist/build-default.umd.development.js')
35+
).toBeTruthy();
36+
expect(
37+
shell.test('-f', 'dist/build-default.umd.development.js')
38+
).toBeTruthy();
39+
40+
expect(shell.test('-f', 'dist/index.d.ts')).toBeTruthy();
41+
42+
expect(output.code).toBe(0);
43+
});
44+
45+
afterEach(() => {
46+
util.teardownStage(stageName);
47+
});
48+
});

test/utils/psKill.js

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
'use strict';
2+
3+
const psTree = require('ps-tree');
4+
5+
// Loops through processes and kills them
6+
module.exports = (pid, signal = 'SIGKILL', callback) => {
7+
psTree(pid, (err, children) => {
8+
let arr = [pid].concat(children.map(p => p.PID));
9+
arr = arr.filter((item, poss) => arr.indexOf(item) === poss);
10+
arr.forEach(tpid => {
11+
try {
12+
process.kill(tpid, signal);
13+
} catch (ex) {
14+
const logger = console;
15+
logger.log('Could not kill process', tpid, ex);
16+
}
17+
});
18+
if (callback) {
19+
callback();
20+
}
21+
});
22+
};

0 commit comments

Comments
 (0)