Skip to content

Commit

Permalink
Add tests and fixtures
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredpalmer committed Apr 30, 2019
1 parent 974805c commit 89c0470
Show file tree
Hide file tree
Showing 13 changed files with 293 additions and 14 deletions.
40 changes: 40 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
version: 2

defaults: &defaults
working_directory: ~/repo
docker:
- image: circleci/node

jobs:
test:
<<: *defaults
steps:
- checkout
- restore_cache:
keys:
- v1-dependencies-{{ checksum "yarn.lock" }}
# fallback to using the latest cache if no exact match is found
- v1-dependencies-

- run:
name: Install and build package
command: yarn install --frozen-lockfile

- run:
name: Run tests
command: yarn test --runInBand --no-cache --coverage

- save_cache:
paths:
- node_modules
key: v1-dependencies-{{ checksum "yarn.lock" }}

- persist_to_workspace:
root: ~/repo
paths: .

workflows:
version: 2
test-deploy:
jobs:
- test
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
},
"scripts": {
"build": "tsc -p tsconfig.json",
"test": "jest --config ./test/jest.config.json",
"prepare": "npm run build"
},
"files": [
Expand Down Expand Up @@ -71,6 +72,8 @@
"husky": "^2.1.0",
"lint-staged": "^8.1.0",
"prettier": "^1.17.0",
"ps-tree": "^1.2.0",
"shelljs": "^0.8.3",
"tslint": "^5.16.0",
"tslint-config-palmerhq": "^1.0.2",
"tslint-config-prettier": "^1.18.0",
Expand Down
2 changes: 1 addition & 1 deletion src/createRollupConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export function createRollupConfig(format, env, opts) {
// Establish Rollup output
output: {
// Set filenames of the consumer's package
file: `${paths.appDist}/${safeVariableName(
file: `${paths.appDist}/${safePackageName(
opts.name
)}.${format}.${env}.js`,
// Pass through the file format
Expand Down
25 changes: 15 additions & 10 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -251,26 +251,31 @@ prog
const [cjsDev, cjsProd, ...otherConfigs] = createBuildConfigs(opts);
if (opts.format.includes('cjs')) {
try {
const promise = fs.writeFile(
resolveApp('dist/index.js'),
`
await mkdirp(resolveApp('./dist'));
const promise = fs
.writeFile(
resolveApp('./dist/index.js'),
`
'use strict'
if (process.env.NODE_ENV === 'production') {
module.exports = require('./${safeVariableName(
module.exports = require('./${safePackageName(
opts.name
)}.cjs.production.js')
} else {
module.exports = require('./${safeVariableName(
module.exports = require('./${safePackageName(
opts.name
)}.cjs.development.js')
}`,
{
overwrite: true,
}
);
{
overwrite: true,
}
)
.catch(e => logError(e));
logger(promise, 'Creating entry file');
} catch (e) {}
} catch (e) {
logError(e);
}
}
try {
const promise = asyncro.map(
Expand Down
7 changes: 7 additions & 0 deletions test/fixtures/build-default/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"scripts": {
"build": "tsdx build"
},
"name": "build-default",
"license": "MIT"
}
6 changes: 6 additions & 0 deletions test/fixtures/build-default/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export const sum = (a: number, b: number) => {
if ('development' === process.env.NODE_ENV) {
console.log('fuck');
}
return a + b;
};
7 changes: 7 additions & 0 deletions test/fixtures/build-default/test/blah.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { sum } from '../src';

describe('fuck', () => {
it('works', () => {
expect(sum(1, 1)).toEqual(2);
});
});
29 changes: 29 additions & 0 deletions test/fixtures/build-default/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"compilerOptions": {
"target": "es5",
"module": "ESNext",
"lib": ["dom", "esnext"],
"declaration": true,
"sourceMap": true,
"rootDir": "./",
"strict": true,
"noImplicitAny": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"strictPropertyInitialization": true,
"noImplicitThis": true,
"alwaysStrict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"moduleResolution": "node",
"baseUrl": "./",
"paths": {
"*": ["src/*", "node_modules/*"]
},
"jsx": "react",
"esModuleInterop": true
},
"include": ["src", "types"],
}
28 changes: 28 additions & 0 deletions test/fixtures/util.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
'use strict';

const shell = require('shelljs');
const path = require('path');
const rootDir = process.cwd();

shell.config.silent = true;

module.exports = {
setupStageWithFixture: (stageName, fixtureName) => {
const stagePath = path.join(rootDir, stageName);
shell.mkdir(stagePath);
shell.exec(`cp -a ${rootDir}/test/fixtures/${fixtureName}/. ${stagePath}/`);
shell.ln(
'-s',
path.join(rootDir, 'node_modules'),
path.join(stagePath, 'node_modules')
);
shell.cd(stagePath);
},

teardownStage: stageName => {
shell.cd(rootDir);
shell.rm('-rf', path.join(rootDir, stageName));
},

rootDir,
};
7 changes: 7 additions & 0 deletions test/jest.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"roots": ["<rootDir>/tests"],
"collectCoverageFrom": ["**/*.js"],
"testMatch": [
"<rootDir>/tests/**/?(*.)(spec|test).(ts|js)?(x)"
]
}
48 changes: 48 additions & 0 deletions test/tests/tsdx-build.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* @jest-environment node
*/
'use strict';

const shell = require('shelljs');
const util = require('../fixtures/util');

shell.config.silent = false;

const stageName = 'stage-build';

describe('tsdx build', () => {
beforeAll(() => {
util.teardownStage(stageName);
});

it('should compile files into a dist directory', () => {
util.setupStageWithFixture(stageName, 'build-default');

const output = shell.exec('node ../dist/index.js build');

expect(shell.test('-f', 'dist/index.js')).toBeTruthy();
expect(
shell.test('-f', 'dist/build-default.cjs.development.js')
).toBeTruthy();
expect(
shell.test('-f', 'dist/build-default.cjs.production.js')
).toBeTruthy();
expect(
shell.test('-f', 'dist/build-default.es.production.js')
).toBeTruthy();
expect(
shell.test('-f', 'dist/build-default.umd.development.js')
).toBeTruthy();
expect(
shell.test('-f', 'dist/build-default.umd.development.js')
).toBeTruthy();

expect(shell.test('-f', 'dist/index.d.ts')).toBeTruthy();

expect(output.code).toBe(0);
});

afterEach(() => {
util.teardownStage(stageName);
});
});
22 changes: 22 additions & 0 deletions test/utils/psKill.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'use strict';

const psTree = require('ps-tree');

// Loops through processes and kills them
module.exports = (pid, signal = 'SIGKILL', callback) => {
psTree(pid, (err, children) => {
let arr = [pid].concat(children.map(p => p.PID));
arr = arr.filter((item, poss) => arr.indexOf(item) === poss);
arr.forEach(tpid => {
try {
process.kill(tpid, signal);
} catch (ex) {
const logger = console;
logger.log('Could not kill process', tpid, ex);
}
});
if (callback) {
callback();
}
});
};
Loading

0 comments on commit 89c0470

Please sign in to comment.