Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(refactor): split build tests into separate files per fixture #621

Merged
merged 2 commits into from
Mar 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
84 changes: 84 additions & 0 deletions test/tests/tsdx-build-default.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
const shell = require('shelljs');
const util = require('../fixtures/util');
const { execWithCache } = require('../utils/shell');

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 = execWithCache('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', () => {
agilgur5 marked this conversation as resolved.
Show resolved Hide resolved
const output = execWithCache('node ../dist/index.js build');

const lib = require(`../../${stageName}/dist`);
expect(lib.foo()).toBe('bar');
expect(lib.__esModule).toBe(true);

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

it('should clean the dist directory before rebuilding', () => {
let output = execWithCache('node ../dist/index.js build');
expect(output.code).toBe(0);

shell.mv('package.json', 'package-og.json');
shell.mv('package2.json', 'package.json');

// cache bust because we want to re-run this command with new package.json
output = execWithCache('node ../dist/index.js build', { noCache: true });
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);
});
});
41 changes: 41 additions & 0 deletions test/tests/tsdx-build-invalid.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const shell = require('shelljs');
const util = require('../fixtures/util');
const { execWithCache } = require('../utils/shell');

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 output = execWithCache('node ../dist/index.js build');
expect(output.code).toBe(1);
});

it('should only transpile and not type check', () => {
const output = execWithCache('node ../dist/index.js build --transpileOnly');

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(output.code).toBe(0);
});

afterAll(() => {
util.teardownStage(stageName);
});
});
68 changes: 68 additions & 0 deletions test/tests/tsdx-build-withTsconfig.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
const shell = require('shelljs');
const util = require('../fixtures/util');
const { execWithCache } = require('../utils/shell');

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 = execWithCache('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 output = execWithCache('node ../dist/index.js build');

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', () => {
const output = execWithCache(
'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);
});
});
164 changes: 0 additions & 164 deletions test/tests/tsdx-build.test.js

This file was deleted.

29 changes: 29 additions & 0 deletions test/utils/shell.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// this file contains helper utils for working with shell.js functions
const shell = require('shelljs');

shell.config.silent = true;

// simple shell.exec "cache" that doesn't re-run the same command twice in a row
let prevCommand = '';
let prevCommandOutput = {};
function execWithCache(command, { noCache = false } = {}) {
// return the old output
if (!noCache && prevCommand === command) return prevCommandOutput;

const output = shell.exec(command);

// reset if command is not to be cached
if (noCache) {
prevCommand = '';
prevCommandOutput = {};
} else {
prevCommand = command;
prevCommandOutput = output;
}

return output;
}

module.exports = {
execWithCache,
};