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 build-default tests for more explicitness and correctness checks #903

Merged
merged 5 commits into from
Oct 14, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions templates/basic/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ This is the folder structure we set up for you:

```txt
/src
index.tsx # EDIT THIS
index.ts # EDIT THIS
/test
blah.test.tsx # EDIT THIS
index.test.ts # EDIT THIS
.gitignore
package.json
README.md # EDIT THIS
Expand Down
2 changes: 1 addition & 1 deletion templates/basic/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export const sum = (a: number, b: number) => {
if ('development' === process.env.NODE_ENV) {
console.log('boop');
console.log('dev only output');
}
return a + b;
};
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { sum } from '../src';

describe('blah', () => {
it('works', () => {
describe('sum', () => {
it('adds two numbers together', () => {
expect(sum(1, 1)).toEqual(2);
});
});
2 changes: 1 addition & 1 deletion templates/react-with-storybook/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ This is the folder structure we set up for you:
/src
index.tsx # EDIT THIS
/test
blah.test.tsx # EDIT THIS
index.test.tsx # EDIT THIS
/stories
Thing.stories.tsx # EDIT THIS
/.storybook
Expand Down
2 changes: 1 addition & 1 deletion templates/react/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ This is the folder structure we set up for you:
/src
index.tsx # EDIT THIS
/test
blah.test.tsx # EDIT THIS
index.test.tsx # EDIT THIS
.gitignore
package.json
README.md # EDIT THIS
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as React from 'react';
import * as ReactDOM from 'react-dom';
import { Thing } from '../src';

describe('it', () => {
describe('Thing', () => {
it('renders without crashing', () => {
const div = document.createElement('div');
ReactDOM.render(<Thing />, div);
Expand Down
1 change: 0 additions & 1 deletion test/e2e/fixtures/build-default/src/foo.ts

This file was deleted.

12 changes: 6 additions & 6 deletions test/e2e/fixtures/build-default/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import './syntax/nullish-coalescing';
import './syntax/optional-chaining';

import './syntax/jsx-import/JSX-import-JSX';

import './syntax/async';
export { testNullishCoalescing } from './syntax/nullish-coalescing';
export { testOptionalChaining } from './syntax/optional-chaining';

export { testGenerator } from './syntax/generator';
export { testAsync } from './syntax/async';

export { kebabCase } from 'lodash';
export { merge, mergeAll } from 'lodash/fp';

export { foo } from './foo';
export { returnsTrue } from './returnsTrue';

export const sum = (a: number, b: number) => {
if ('development' === process.env.NODE_ENV) {
console.log('fuck');
console.log('dev only output');
}
return a + b;
};
2 changes: 2 additions & 0 deletions test/e2e/fixtures/build-default/src/returnsTrue.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// this just ensure a simple import works
export const returnsTrue = () => true;
7 changes: 6 additions & 1 deletion test/e2e/fixtures/build-default/src/syntax/async.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
// regression test for async/await
// code inspired by https://github.com/formium/tsdx/issues/869
let shouldBeTrue = false;
(async () => {
shouldBeTrue = true; // a side effect to make sure this is output
await Promise.resolve();
console.log('a side effect to make sure this is output');
})();

export async function testAsync() {
return await Promise.resolve(shouldBeTrue);
}
2 changes: 1 addition & 1 deletion test/e2e/fixtures/build-default/src/syntax/generator.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// regression test for generators
export function* testGenerator() {
return yield 'blah';
return yield true;
}
10 changes: 7 additions & 3 deletions test/e2e/fixtures/build-default/src/syntax/nullish-coalescing.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
// regression test for nullish coalescing syntax
// https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html#nullish-coalescing

const bar = () => {};
const foo = false;
export const x = foo ?? bar();
export function testNullishCoalescing() {
const someFunc = () => 'some string';
const someFalse = false;
const shouldBeTrue = !(someFalse ?? someFunc());
return shouldBeTrue;
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
// regression test for optional chaining syntax
// https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html#optional-chaining

export const foo = (foo?: { bar: string }) => foo?.bar || 'bar';
export function testOptionalChaining() {
const someObj: { someOptionalString?: string } = {};
const shouldBeTrue = someObj?.someOptionalString || true;
return shouldBeTrue;
}
2 changes: 1 addition & 1 deletion test/e2e/fixtures/build-withTsconfig/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export const sum = (a: number, b: number) => {
if ('development' === process.env.NODE_ENV) {
console.log('fuck');
console.log('dev only output');
}
return a + b;
};
21 changes: 10 additions & 11 deletions test/e2e/tsdx-build-default.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,19 @@ describe('tsdx build :: zero-config defaults', () => {
expect(output.code).toBe(0);
});

it('should create the library correctly', () => {
it('should create the library correctly', async () => {
const output = execWithCache('node ../dist/index.js build');

const lib = require(`../../${stageName}/dist`);
expect(lib.foo()).toBe('bar');
expect(lib.__esModule).toBe(true);
expect(lib.returnsTrue()).toBe(true);
expect(lib.__esModule).toBe(true); // test that ESM -> CJS interop was output

// syntax tests
expect(lib.testNullishCoalescing()).toBe(true);
expect(lib.testOptionalChaining()).toBe(true);
// can't use an async generator in Jest yet, so use next().value instead of yield
expect(lib.testGenerator().next().value).toBe(true);
expect(await lib.testAsync()).toBe(true);

expect(output.code).toBe(0);
});
Expand All @@ -59,14 +66,6 @@ describe('tsdx build :: zero-config defaults', () => {
expect(matched).toBeTruthy();
});

it('should not bundle regeneratorRuntime when targeting Node', () => {
const output = execWithCache('node ../dist/index.js build --target node');
expect(output.code).toBe(0);

const matched = grep(/regeneratorRuntime = r/, ['dist/build-default.*.js']);
expect(matched).toBeFalsy();
});

it('should use lodash for the CJS build', () => {
const output = execWithCache('node ../dist/index.js build');
expect(output.code).toBe(0);
Expand Down
10 changes: 9 additions & 1 deletion test/e2e/tsdx-build-options.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as shell from 'shelljs';

import * as util from '../utils/fixture';
import { execWithCache } from '../utils/shell';
import { execWithCache, grep } from '../utils/shell';

shell.config.silent = false;

Expand Down Expand Up @@ -47,6 +47,14 @@ describe('tsdx build :: options', () => {
expect(output.code).toBe(0);
});

it('should not bundle regeneratorRuntime when targeting Node', () => {
const output = execWithCache('node ../dist/index.js build --target node');
expect(output.code).toBe(0);

const matched = grep(/regeneratorRuntime = r/, ['dist/build-default.*.js']);
expect(matched).toBeFalsy();
});

afterAll(() => {
util.teardownStage(stageName);
});
Expand Down
2 changes: 1 addition & 1 deletion test/integration/fixtures/build-options/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ warning(true, 'warning - water is wet');

export const sum = (a: number, b: number) => {
if ('development' === process.env.NODE_ENV) {
console.log('fuck');
console.log('dev only output');
}
return a + b;
};
2 changes: 1 addition & 1 deletion test/integration/fixtures/build-withBabel/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ export { Title } from './styled';

export const sum = (a: number, b: number) => {
if ('development' === process.env.NODE_ENV) {
console.log('fuck');
console.log('dev only output');
}
return a + b;
};
2 changes: 1 addition & 1 deletion test/integration/fixtures/build-withConfig/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import './index.css';

export const sum = (a: number, b: number) => {
if ('development' === process.env.NODE_ENV) {
console.log('fuck');
console.log('dev only output');
}
return a + b;
};