diff --git a/test/tests/tsdx-build-default.test.js b/test/tests/tsdx-build-default.test.js new file mode 100644 index 000000000..5650e5417 --- /dev/null +++ b/test/tests/tsdx-build-default.test.js @@ -0,0 +1,75 @@ +const shell = require('shelljs'); +const util = require('../fixtures/util'); + +shell.config.silent = false; + +const fixtureName = 'build-default'; +const stageName = `stage-${fixtureName}`; + +describe('tsdx build :: zero-config defaults', () => { + beforeAll(() => { + util.teardownStage(stageName); + util.setupStageWithFixture(stageName, fixtureName); + }); + + it('should compile files into a dist directory', () => { + 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.min.js') + ).toBeTruthy(); + expect(shell.test('-f', 'dist/build-default.esm.js')).toBeTruthy(); + + expect(shell.test('-f', 'dist/index.d.ts')).toBeTruthy(); + + expect(output.code).toBe(0); + }); + + it('should create the library correctly', () => { + const lib = require(`../../${stageName}/dist`); + expect(lib.foo()).toBe('bar'); + expect(lib.__esModule).toBe(true); + }); + + it('should clean the dist directory before rebuilding', () => { + shell.mv('package.json', 'package-og.json'); + shell.mv('package2.json', 'package.json'); + + const output = shell.exec('node ../dist/index.js build'); + expect(shell.test('-f', 'dist/index.js')).toBeTruthy(); + + // build-default files have been cleaned out + expect( + shell.test('-f', 'dist/build-default.cjs.development.js') + ).toBeFalsy(); + expect( + shell.test('-f', 'dist/build-default.cjs.production.min.js') + ).toBeFalsy(); + expect(shell.test('-f', 'dist/build-default.esm.js')).toBeFalsy(); + + // build-default-2 files have been added + expect( + shell.test('-f', 'dist/build-default-2.cjs.development.js') + ).toBeTruthy(); + expect( + shell.test('-f', 'dist/build-default-2.cjs.production.min.js') + ).toBeTruthy(); + expect(shell.test('-f', 'dist/build-default-2.esm.js')).toBeTruthy(); + + expect(shell.test('-f', 'dist/index.d.ts')).toBeTruthy(); + + expect(output.code).toBe(0); + + // reset package.json files + shell.mv('package.json', 'package2.json'); + shell.mv('package-og.json', 'package.json'); + }); + + afterAll(() => { + util.teardownStage(stageName); + }); +}); diff --git a/test/tests/tsdx-build-invalid.test.js b/test/tests/tsdx-build-invalid.test.js new file mode 100644 index 000000000..164b63845 --- /dev/null +++ b/test/tests/tsdx-build-invalid.test.js @@ -0,0 +1,40 @@ +const shell = require('shelljs'); +const util = require('../fixtures/util'); + +shell.config.silent = false; + +const fixtureName = 'build-invalid'; +const stageName = `stage-${fixtureName}`; + +describe('tsdx build :: invalid build', () => { + beforeAll(() => { + util.teardownStage(stageName); + util.setupStageWithFixture(stageName, fixtureName); + }); + + it('should fail gracefully with exit code 1 when build failed', () => { + const code = shell.exec('node ../dist/index.js build').code; + expect(code).toBe(1); + }); + + it('should only transpile and not type check', () => { + const code = shell.exec('node ../dist/index.js build --transpileOnly').code; + + expect(shell.test('-f', 'dist/index.js')).toBeTruthy(); + expect( + shell.test('-f', 'dist/build-invalid.cjs.development.js') + ).toBeTruthy(); + expect( + shell.test('-f', 'dist/build-invalid.cjs.production.min.js') + ).toBeTruthy(); + expect(shell.test('-f', 'dist/build-invalid.esm.js')).toBeTruthy(); + + expect(shell.test('-f', 'dist/index.d.ts')).toBeTruthy(); + + expect(code).toBe(0); + }); + + afterAll(() => { + util.teardownStage(stageName); + }); +}); diff --git a/test/tests/tsdx-build-withTsconfig.test.js b/test/tests/tsdx-build-withTsconfig.test.js new file mode 100644 index 000000000..be4d72567 --- /dev/null +++ b/test/tests/tsdx-build-withTsconfig.test.js @@ -0,0 +1,63 @@ +const shell = require('shelljs'); +const util = require('../fixtures/util'); + +shell.config.silent = false; + +const fixtureName = 'build-withTsconfig'; +const stageName = `stage-${fixtureName}`; + +describe('tsdx build :: build with custom tsconfig.json options', () => { + beforeAll(() => { + util.teardownStage(stageName); + util.setupStageWithFixture(stageName, fixtureName); + }); + + it('should use the declarationDir when set', () => { + const output = shell.exec('node ../dist/index.js build'); + + expect(shell.test('-f', 'dist/index.js')).toBeTruthy(); + expect( + shell.test('-f', 'dist/build-withtsconfig.cjs.development.js') + ).toBeTruthy(); + expect( + shell.test('-f', 'dist/build-withtsconfig.cjs.production.min.js') + ).toBeTruthy(); + expect(shell.test('-f', 'dist/build-withtsconfig.esm.js')).toBeTruthy(); + + expect(shell.test('-f', 'dist/index.d.ts')).toBeFalsy(); + expect(shell.test('-f', 'typings/index.d.ts')).toBeTruthy(); + expect(shell.test('-f', 'typings/index.d.ts.map')).toBeTruthy(); + + expect(output.code).toBe(0); + }); + + it('should set __esModule according to esModuleInterop', () => { + const lib = require(`../../${stageName}/dist/build-withtsconfig.cjs.production.min.js`); + // if esModuleInterop: false, no __esModule is added, therefore undefined + expect(lib.__esModule).toBe(undefined); + }); + + it('should read custom --tsconfig path', () => { + const output = shell.exec( + 'node ../dist/index.js build --format cjs --tsconfig ./src/tsconfig.json' + ); + + expect(shell.test('-f', 'dist/index.js')).toBeTruthy(); + expect( + shell.test('-f', 'dist/build-withtsconfig.cjs.development.js') + ).toBeTruthy(); + expect( + shell.test('-f', 'dist/build-withtsconfig.cjs.production.min.js') + ).toBeTruthy(); + + expect(shell.test('-f', 'dist/index.d.ts')).toBeFalsy(); + expect(shell.test('-f', 'typingsCustom/index.d.ts')).toBeTruthy(); + expect(shell.test('-f', 'typingsCustom/index.d.ts.map')).toBeTruthy(); + + expect(output.code).toBe(0); + }); + + afterAll(() => { + util.teardownStage(stageName); + }); +}); diff --git a/test/tests/tsdx-build.test.js b/test/tests/tsdx-build.test.js deleted file mode 100644 index b1663c658..000000000 --- a/test/tests/tsdx-build.test.js +++ /dev/null @@ -1,164 +0,0 @@ -/** - * @jest-environment node - */ - -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 --format esm,cjs'); - - 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.min.js') - ).toBeTruthy(); - expect(shell.test('-f', 'dist/build-default.esm.js')).toBeTruthy(); - - expect(shell.test('-f', 'dist/index.d.ts')).toBeTruthy(); - - expect(output.code).toBe(0); - }); - - it('should create the library correctly', () => { - util.setupStageWithFixture(stageName, 'build-default'); - - shell.exec('node ../dist/index.js build'); - - const lib = require(`../../${stageName}/dist`); - expect(lib.foo()).toBe('bar'); - expect(lib.__esModule).toBe(true); - }); - - it('should clean the dist directory before rebuilding', () => { - util.setupStageWithFixture(stageName, 'build-default'); - - shell.mv('package.json', 'package-og.json'); - shell.mv('package2.json', 'package.json'); - - const output = shell.exec('node ../dist/index.js build --format esm,cjs'); - expect(shell.test('-f', 'dist/index.js')).toBeTruthy(); - - // build-default files have been cleaned out - expect( - shell.test('-f', 'dist/build-default.cjs.development.js') - ).toBeFalsy(); - expect( - shell.test('-f', 'dist/build-default.cjs.production.min.js') - ).toBeFalsy(); - expect(shell.test('-f', 'dist/build-default.esm.js')).toBeFalsy(); - - // build-default-2 files have been added - expect( - shell.test('-f', 'dist/build-default-2.cjs.development.js') - ).toBeTruthy(); - expect( - shell.test('-f', 'dist/build-default-2.cjs.production.min.js') - ).toBeTruthy(); - expect(shell.test('-f', 'dist/build-default-2.esm.js')).toBeTruthy(); - - expect(shell.test('-f', 'dist/index.d.ts')).toBeTruthy(); - - expect(output.code).toBe(0); - - // reset package.json files - shell.mv('package.json', 'package2.json'); - shell.mv('package-og.json', 'package.json'); - }); - - it('should fail gracefully with exit code 1 when build failed', () => { - util.setupStageWithFixture(stageName, 'build-invalid'); - const code = shell.exec('node ../dist/index.js build').code; - expect(code).toBe(1); - }); - - it('should only transpile and not type check', () => { - util.setupStageWithFixture(stageName, 'build-invalid'); - const code = shell.exec('node ../dist/index.js build --transpileOnly').code; - - expect(shell.test('-f', 'dist/index.js')).toBeTruthy(); - expect( - shell.test('-f', 'dist/build-invalid.cjs.development.js') - ).toBeTruthy(); - expect( - shell.test('-f', 'dist/build-invalid.cjs.production.min.js') - ).toBeTruthy(); - expect(shell.test('-f', 'dist/build-invalid.esm.js')).toBeTruthy(); - - expect(shell.test('-f', 'dist/index.d.ts')).toBeTruthy(); - - expect(code).toBe(0); - }); - - it('should use the declarationDir when set in tsconfig', () => { - util.setupStageWithFixture(stageName, 'build-withTsconfig'); - - const output = shell.exec('node ../dist/index.js build --format esm,cjs'); - - expect(shell.test('-f', 'dist/index.js')).toBeTruthy(); - expect( - shell.test('-f', 'dist/build-withtsconfig.cjs.development.js') - ).toBeTruthy(); - expect( - shell.test('-f', 'dist/build-withtsconfig.cjs.production.min.js') - ).toBeTruthy(); - expect(shell.test('-f', 'dist/build-withtsconfig.esm.js')).toBeTruthy(); - - expect(shell.test('-f', 'dist/index.d.ts')).toBeFalsy(); - expect(shell.test('-f', 'typings/index.d.ts')).toBeTruthy(); - expect(shell.test('-f', 'typings/index.d.ts.map')).toBeTruthy(); - - expect(output.code).toBe(0); - }); - - it('should set __esModule according to esModuleInterop in tsconfig', () => { - util.setupStageWithFixture(stageName, 'build-withTsconfig'); - - const output = shell.exec('node ../dist/index.js build --format cjs'); - - const lib = require(`../../${stageName}/dist/build-withtsconfig.cjs.production.min.js`); - // if esModuleInterop: false, no __esModule is added, therefore undefined - expect(lib.__esModule).toBe(undefined); - - expect(output.code).toBe(0); - }); - - it('should read custom --tsconfig path', () => { - util.setupStageWithFixture(stageName, 'build-withTsconfig'); - - const output = shell.exec( - 'node ../dist/index.js build --format cjs --tsconfig ./src/tsconfig.json' - ); - - expect(shell.test('-f', 'dist/index.js')).toBeTruthy(); - expect( - shell.test('-f', 'dist/build-withtsconfig.cjs.development.js') - ).toBeTruthy(); - expect( - shell.test('-f', 'dist/build-withtsconfig.cjs.production.min.js') - ).toBeTruthy(); - - expect(shell.test('-f', 'dist/index.d.ts')).toBeFalsy(); - expect(shell.test('-f', 'typingsCustom/index.d.ts')).toBeTruthy(); - expect(shell.test('-f', 'typingsCustom/index.d.ts.map')).toBeTruthy(); - - expect(output.code).toBe(0); - }); - - afterEach(() => { - util.teardownStage(stageName); - }); -});