Skip to content

Commit

Permalink
refactor/test: test for correctness of syntax, not just parsing
Browse files Browse the repository at this point in the history
- have most of the syntax tests export a function that returns true
  if the syntax is evaluated correctly, and test that it does in fact
  return true
  - just using `true` as something consistent and easy to test for

- change the async test to move the side effect before the Promise
  resolves
  - not entirely sure why, potentially because of the way the event
    loop works (race condition), but this was returning false when
    after and true when before
    - both work as a side effect, so just have it before
  • Loading branch information
agilgur5 committed Oct 14, 2020
1 parent 1c14079 commit 30d69d9
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 12 deletions.
8 changes: 4 additions & 4 deletions test/e2e/fixtures/build-default/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
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';
Expand Down
6 changes: 5 additions & 1 deletion test/e2e/fixtures/build-default/src/syntax/async.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
// code inspired by https://github.com/formium/tsdx/issues/869
let shouldBeTrue = false;
(async () => {
await Promise.resolve();
shouldBeTrue = true; // a side effect to make sure this is output
await Promise.resolve();
})();

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;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
// regression test for nullish coalescing syntax
// https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html#nullish-coalescing

const someFunc = () => {};
const someFalse = false;
const shouldBeFalse = someFalse ?? someFunc();
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,5 +1,8 @@
// regression test for optional chaining syntax
// https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html#optional-chaining

const someObj: { someOptionalString?: string } = {};
const shouldBeBar = someObj?.someOptionalString || 'bar';
export function testOptionalChaining() {
const someObj: { someOptionalString?: string } = {};
const shouldBeTrue = someObj?.someOptionalString || true;
return shouldBeTrue;
}
9 changes: 8 additions & 1 deletion test/e2e/tsdx-build-default.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,20 @@ 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.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 Down

1 comment on commit 30d69d9

@vercel

This comment was marked as resolved.

Please sign in to comment.