From 89c0470fef6f664bf8a8ce4b7d233395f42f62f1 Mon Sep 17 00:00:00 2001 From: Jared Palmer Date: Tue, 30 Apr 2019 11:56:52 -0400 Subject: [PATCH] Add tests and fixtures --- .circleci/config.yml | 40 +++++++++ package.json | 3 + src/createRollupConfig.js | 2 +- src/index.js | 25 +++--- test/fixtures/build-default/package.json | 7 ++ test/fixtures/build-default/src/index.ts | 6 ++ test/fixtures/build-default/test/blah.test.ts | 7 ++ test/fixtures/build-default/tsconfig.json | 29 +++++++ test/fixtures/util.js | 28 +++++++ test/jest.config.json | 7 ++ test/tests/tsdx-build.test.js | 48 +++++++++++ test/utils/psKill.js | 22 +++++ yarn.lock | 83 ++++++++++++++++++- 13 files changed, 293 insertions(+), 14 deletions(-) create mode 100644 .circleci/config.yml create mode 100644 test/fixtures/build-default/package.json create mode 100644 test/fixtures/build-default/src/index.ts create mode 100644 test/fixtures/build-default/test/blah.test.ts create mode 100644 test/fixtures/build-default/tsconfig.json create mode 100644 test/fixtures/util.js create mode 100644 test/jest.config.json create mode 100644 test/tests/tsdx-build.test.js create mode 100644 test/utils/psKill.js diff --git a/.circleci/config.yml b/.circleci/config.yml new file mode 100644 index 000000000..e56934a85 --- /dev/null +++ b/.circleci/config.yml @@ -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 diff --git a/package.json b/package.json index c3a4906fd..fd8502f4f 100644 --- a/package.json +++ b/package.json @@ -22,6 +22,7 @@ }, "scripts": { "build": "tsc -p tsconfig.json", + "test": "jest --config ./test/jest.config.json", "prepare": "npm run build" }, "files": [ @@ -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", diff --git a/src/createRollupConfig.js b/src/createRollupConfig.js index 2e51826ab..1768ee8da 100644 --- a/src/createRollupConfig.js +++ b/src/createRollupConfig.js @@ -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 diff --git a/src/index.js b/src/index.js index b7549ac92..d7d99b306 100755 --- a/src/index.js +++ b/src/index.js @@ -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( diff --git a/test/fixtures/build-default/package.json b/test/fixtures/build-default/package.json new file mode 100644 index 000000000..45de7c92f --- /dev/null +++ b/test/fixtures/build-default/package.json @@ -0,0 +1,7 @@ +{ + "scripts": { + "build": "tsdx build" + }, + "name": "build-default", + "license": "MIT" +} \ No newline at end of file diff --git a/test/fixtures/build-default/src/index.ts b/test/fixtures/build-default/src/index.ts new file mode 100644 index 000000000..af27ae37d --- /dev/null +++ b/test/fixtures/build-default/src/index.ts @@ -0,0 +1,6 @@ +export const sum = (a: number, b: number) => { + if ('development' === process.env.NODE_ENV) { + console.log('fuck'); + } + return a + b; +}; diff --git a/test/fixtures/build-default/test/blah.test.ts b/test/fixtures/build-default/test/blah.test.ts new file mode 100644 index 000000000..c14b0da80 --- /dev/null +++ b/test/fixtures/build-default/test/blah.test.ts @@ -0,0 +1,7 @@ +import { sum } from '../src'; + +describe('fuck', () => { + it('works', () => { + expect(sum(1, 1)).toEqual(2); + }); +}); diff --git a/test/fixtures/build-default/tsconfig.json b/test/fixtures/build-default/tsconfig.json new file mode 100644 index 000000000..ecb27d608 --- /dev/null +++ b/test/fixtures/build-default/tsconfig.json @@ -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"], +} \ No newline at end of file diff --git a/test/fixtures/util.js b/test/fixtures/util.js new file mode 100644 index 000000000..5223cc9aa --- /dev/null +++ b/test/fixtures/util.js @@ -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, +}; diff --git a/test/jest.config.json b/test/jest.config.json new file mode 100644 index 000000000..db2c712e2 --- /dev/null +++ b/test/jest.config.json @@ -0,0 +1,7 @@ +{ + "roots": ["/tests"], + "collectCoverageFrom": ["**/*.js"], + "testMatch": [ + "/tests/**/?(*.)(spec|test).(ts|js)?(x)" + ] +} \ No newline at end of file diff --git a/test/tests/tsdx-build.test.js b/test/tests/tsdx-build.test.js new file mode 100644 index 000000000..6766ee66c --- /dev/null +++ b/test/tests/tsdx-build.test.js @@ -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); + }); +}); diff --git a/test/utils/psKill.js b/test/utils/psKill.js new file mode 100644 index 000000000..9d823d05b --- /dev/null +++ b/test/utils/psKill.js @@ -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(); + } + }); +}; diff --git a/yarn.lock b/yarn.lock index 7f798602f..75a649697 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1806,7 +1806,7 @@ domexception@^1.0.1: dependencies: webidl-conversions "^4.0.2" -duplexer@^0.1.1: +duplexer@^0.1.1, duplexer@~0.1.1: version "0.1.1" resolved "https://registry.yarnpkg.com/duplexer/-/duplexer-0.1.1.tgz#ace6ff808c1ce66b57d1ebf97977acb02334cfc1" integrity sha1-rOb/gIwc5mtX0ev5eXessCM0z8E= @@ -1965,6 +1965,19 @@ esutils@^2.0.2: resolved "https://registry.yarnpkg.com/esutils/-/esutils-2.0.2.tgz#0abf4f1caa5bcb1f7a9d8acc6dea4faaa04bac9b" integrity sha1-Cr9PHKpbyx96nYrMbepPqqBLrJs= +event-stream@=3.3.4: + version "3.3.4" + resolved "https://registry.yarnpkg.com/event-stream/-/event-stream-3.3.4.tgz#4ab4c9a0f5a54db9338b4c34d86bfce8f4b35571" + integrity sha1-SrTJoPWlTbkzi0w02Gv86PSzVXE= + dependencies: + duplexer "~0.1.1" + from "~0" + map-stream "~0.1.0" + pause-stream "0.0.11" + split "0.3" + stream-combiner "~0.0.4" + through "~2.3.1" + events@^3.0.0: version "3.0.0" resolved "https://registry.yarnpkg.com/events/-/events-3.0.0.tgz#9a0a0dfaf62893d92b875b8f2698ca4114973e88" @@ -2237,6 +2250,11 @@ from2@^2.1.0: inherits "^2.0.1" readable-stream "^2.0.0" +from@~0: + version "0.1.7" + resolved "https://registry.yarnpkg.com/from/-/from-0.1.7.tgz#83c60afc58b9c56997007ed1a768b3ab303a44fe" + integrity sha1-g8YK/Fi5xWmXAH7Rp2izqzA6RP4= + fs-extra@7.0.1, fs-extra@^7.0.1: version "7.0.1" resolved "https://registry.yarnpkg.com/fs-extra/-/fs-extra-7.0.1.tgz#4f189c44aa123b895f722804f55ea23eadc348e9" @@ -2369,7 +2387,7 @@ glob-parent@^3.1.0: is-glob "^3.1.0" path-dirname "^1.0.0" -glob@^7.0.3, glob@^7.1.1, glob@^7.1.2, glob@^7.1.3: +glob@^7.0.0, glob@^7.0.3, glob@^7.1.1, glob@^7.1.2, glob@^7.1.3: version "7.1.3" resolved "https://registry.yarnpkg.com/glob/-/glob-7.1.3.tgz#3960832d3f1574108342dafd3a67b332c0969df1" integrity sha512-vcfuiIxogLV4DlGBHIUOwI0IbrJ8HWPc4MU7HzviGeNho/UJDfi6B5p3sHeWIQ0KGIU0Jpxi5ZHxemQfLkkAwQ== @@ -2659,6 +2677,11 @@ ini@~1.3.0: resolved "https://registry.yarnpkg.com/ini/-/ini-1.3.5.tgz#eee25f56db1c9ec6085e0c22778083f596abf927" integrity sha512-RZY5huIKCMRWDUqZlEi72f/lmXKMvuszcMBduliQ3nnWbx9X/ZBQO7DijMEYS9EhHBb2qacRUMtC7svLwe0lcw== +interpret@^1.0.0: + version "1.2.0" + resolved "https://registry.yarnpkg.com/interpret/-/interpret-1.2.0.tgz#d5061a6224be58e8083985f5014d844359576296" + integrity sha512-mT34yGKMNceBQUoVn7iCDKDntA7SC6gycMAWzGx1z/CMCTV7b2AAtXlo3nRyHZ1FelRkQbQjprHSYGwzLtkVbw== + invariant@^2.2.4: version "2.2.4" resolved "https://registry.yarnpkg.com/invariant/-/invariant-2.2.4.tgz#610f3c92c9359ce1db616e538008d23ff35158e6" @@ -3843,6 +3866,11 @@ map-cache@^0.2.2: resolved "https://registry.yarnpkg.com/map-cache/-/map-cache-0.2.2.tgz#c32abd0bd6525d9b051645bb4f26ac5dc98a0dbf" integrity sha1-wyq9C9ZSXZsFFkW7TyasXcmKDb8= +map-stream@~0.1.0: + version "0.1.0" + resolved "https://registry.yarnpkg.com/map-stream/-/map-stream-0.1.0.tgz#e56aa94c4c8055a16404a0674b78f215f7c8e194" + integrity sha1-5WqpTEyAVaFkBKBnS3jyFffI4ZQ= + map-visit@^1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/map-visit/-/map-visit-1.0.0.tgz#ecdca8f13144e660f1b5bd41f12f3479d98dfb8f" @@ -4575,6 +4603,13 @@ path-type@^3.0.0: dependencies: pify "^3.0.0" +pause-stream@0.0.11: + version "0.0.11" + resolved "https://registry.yarnpkg.com/pause-stream/-/pause-stream-0.0.11.tgz#fe5a34b0cbce12b5aa6a2b403ee2e73b602f1445" + integrity sha1-/lo0sMvOErWqaitAPuLnO2AvFEU= + dependencies: + through "~2.3" + pbkdf2@^3.0.3: version "3.0.17" resolved "https://registry.yarnpkg.com/pbkdf2/-/pbkdf2-3.0.17.tgz#976c206530617b14ebb32114239f7b09336e93a6" @@ -4727,6 +4762,13 @@ prr@~1.0.1: resolved "https://registry.yarnpkg.com/prr/-/prr-1.0.1.tgz#d3fc114ba06995a45ec6893f484ceb1d78f5f476" integrity sha1-0/wRS6BplaRexok/SEzrHXj19HY= +ps-tree@^1.2.0: + version "1.2.0" + resolved "https://registry.yarnpkg.com/ps-tree/-/ps-tree-1.2.0.tgz#5e7425b89508736cdd4f2224d028f7bb3f722ebd" + integrity sha512-0VnamPPYHl4uaU/nSFeZZpR21QAWRz+sRv4iW9+v/GS/J5U5iZB5BNN6J0RMoOvdx2gWM2+ZFMIm58q24e4UYA== + dependencies: + event-stream "=3.3.4" + pseudomap@^1.0.2: version "1.0.2" resolved "https://registry.yarnpkg.com/pseudomap/-/pseudomap-1.0.2.tgz#f052a28da70e618917ef0a8ac34c1ae5a68286b3" @@ -4899,6 +4941,13 @@ realpath-native@^1.1.0: dependencies: util.promisify "^1.0.0" +rechoir@^0.6.2: + version "0.6.2" + resolved "https://registry.yarnpkg.com/rechoir/-/rechoir-0.6.2.tgz#85204b54dba82d5742e28c96756ef43af50e3384" + integrity sha1-hSBLVNuoLVdC4oyWdW70OvUOM4Q= + dependencies: + resolve "^1.1.6" + regex-cache@^0.4.2: version "0.4.4" resolved "https://registry.yarnpkg.com/regex-cache/-/regex-cache-0.4.4.tgz#75bdc58a2a1496cec48a12835bc54c8d562336dd" @@ -5015,7 +5064,7 @@ resolve@1.10.0, resolve@1.x: dependencies: path-parse "^1.0.6" -resolve@^1.10.0: +resolve@^1.1.6, resolve@^1.10.0: version "1.10.1" resolved "https://registry.yarnpkg.com/resolve/-/resolve-1.10.1.tgz#664842ac960795bbe758221cdccda61fb64b5f18" integrity sha512-KuIe4mf++td/eFb6wkaPbMDnP6kObCaEtIDuHOUED6MNUo4K670KZUHuuvYPZDxNF0WVLw49n06M2m2dXphEzA== @@ -5342,6 +5391,15 @@ shebang-regex@^1.0.0: resolved "https://registry.yarnpkg.com/shebang-regex/-/shebang-regex-1.0.0.tgz#da42f49740c0b42db2ca9728571cb190c98efea3" integrity sha1-2kL0l0DAtC2yypcoVxyxkMmO/qM= +shelljs@^0.8.3: + version "0.8.3" + resolved "https://registry.yarnpkg.com/shelljs/-/shelljs-0.8.3.tgz#a7f3319520ebf09ee81275b2368adb286659b097" + integrity sha512-fc0BKlAWiLpwZljmOvAOTE/gXawtCoNrP5oaY7KIaQbbyHeQVg01pSEuEGvGh3HEdBU4baCD7wQBwADmM/7f7A== + dependencies: + glob "^7.0.0" + interpret "^1.0.0" + rechoir "^0.6.2" + shellwords@^0.1.1: version "0.1.1" resolved "https://registry.yarnpkg.com/shellwords/-/shellwords-0.1.1.tgz#d6b9181c1a48d397324c84871efbcfc73fc0654b" @@ -5489,6 +5547,13 @@ split-string@^3.0.1, split-string@^3.0.2: dependencies: extend-shallow "^3.0.0" +split@0.3: + version "0.3.3" + resolved "https://registry.yarnpkg.com/split/-/split-0.3.3.tgz#cd0eea5e63a211dfff7eb0f091c4133e2d0dd28f" + integrity sha1-zQ7qXmOiEd//frDwkcQTPi0N0o8= + dependencies: + through "2" + sprintf-js@~1.0.2: version "1.0.3" resolved "https://registry.yarnpkg.com/sprintf-js/-/sprintf-js-1.0.3.tgz#04e6926f662895354f3dd015203633b857297e2c" @@ -5547,6 +5612,13 @@ stream-browserify@^2.0.1: inherits "~2.0.1" readable-stream "^2.0.2" +stream-combiner@~0.0.4: + version "0.0.4" + resolved "https://registry.yarnpkg.com/stream-combiner/-/stream-combiner-0.0.4.tgz#4d5e433c185261dde623ca3f44c586bcf5c4ad14" + integrity sha1-TV5DPBhSYd3mI8o/RMWGvPXErRQ= + dependencies: + duplexer "~0.1.1" + stream-each@^1.1.0: version "1.2.3" resolved "https://registry.yarnpkg.com/stream-each/-/stream-each-1.2.3.tgz#ebe27a0c389b04fbcc233642952e10731afa9bae" @@ -5753,6 +5825,11 @@ through2@^2.0.0: readable-stream "~2.3.6" xtend "~4.0.1" +through@2, through@~2.3, through@~2.3.1: + version "2.3.8" + resolved "https://registry.yarnpkg.com/through/-/through-2.3.8.tgz#0dd4c9ffaabc357960b1b724115d7e0e86a2e1f5" + integrity sha1-DdTJ/6q8NXlgsbckEV1+Doai4fU= + timers-browserify@^2.0.4: version "2.0.10" resolved "https://registry.yarnpkg.com/timers-browserify/-/timers-browserify-2.0.10.tgz#1d28e3d2aadf1d5a5996c4e9f95601cd053480ae"