Skip to content

Commit

Permalink
test(preset): fixes tests, doc and cli for presets
Browse files Browse the repository at this point in the history
  • Loading branch information
huafu committed Oct 6, 2018
1 parent ec78757 commit 7b2dd01
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 9 deletions.
12 changes: 5 additions & 7 deletions docs/user/config/index.md
Expand Up @@ -54,20 +54,18 @@ module.exports = {

Any preset can also be used with other options.
If you're already using another preset, you might want only some specific settings from the chosen `ts-jest` preset.
In this case you'll need to use the JavaScript version of Jest config:
In this case you'll need to use the JavaScript version of Jest config (comment/uncomment according to your use-case):

```js
// jest.config.js
const tsJestPresets = require('ts-jest/presets');

const preset = tsJestPresets.defaults
// const preset = tsJestPresets.jsWithTs
// const preset = tsJestPresets.jsWithBabel
const { defaults: tsjPreset } = require('ts-jest/presets');
// const { jsWithTs: tsjPreset } = require('ts-jest/presets');
// const { jsWithBabel: tsjPreset } = require('ts-jest/presets');

module.exports = {
// [...]
transform: {
...preset.transform,
...tsjPreset.transform,
// [...]
}
}
Expand Down
63 changes: 63 additions & 0 deletions e2e/__tests__/__snapshots__/test-helpers.test.ts.snap
@@ -0,0 +1,63 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`test-helpers 1`] = `
× jest --no-cache
↳ exit code: 1
===[ STDOUT ]===================================================================
===[ STDERR ]===================================================================
PASS ./pass.spec.ts
FAIL ./fail.spec.ts
● Test suite failed to run
TypeScript diagnostics (customize using \`[jest-config].globals.ts-jest.diagnostics\` option):
fail.spec.ts:7:10 - error TS2554: Expected 0 arguments, but got 1.
7 expect(mocked(foo)('hello')).toBeUndefined()
~~~~~~~~~~~~~~~~~~~~
fail.spec.ts:9:46 - error TS2345: Argument of type '42' is not assignable to parameter of type 'string'.
9 expect(mocked(bar, true).dummy.deep.deeper(42)).toBeUndefined()
~~
ts-jest[root] (WARN) The \`mocked\` helper has been moved to \`ts-jest/utils\`. Use \`import { mocked } from 'ts-jest/utils'\` instead.
PASS ./deprecated.spec.ts
Test Suites: 1 failed, 2 passed, 3 total
Tests: 3 passed, 3 total
Snapshots: 0 total
Time: XXs
Ran all test suites.
================================================================================
`;

exports[`with esModuleInterop set to false 1`] = `
× jest --no-cache
↳ exit code: 1
===[ STDOUT ]===================================================================
===[ STDERR ]===================================================================
PASS ./pass.spec.ts
FAIL ./fail.spec.ts
● Test suite failed to run
TypeScript diagnostics (customize using \`[jest-config].globals.ts-jest.diagnostics\` option):
fail.spec.ts:7:10 - error TS2554: Expected 0 arguments, but got 1.
7 expect(mocked(foo)('hello')).toBeUndefined()
~~~~~~~~~~~~~~~~~~~~
fail.spec.ts:9:46 - error TS2345: Argument of type '42' is not assignable to parameter of type 'string'.
9 expect(mocked(bar, true).dummy.deep.deeper(42)).toBeUndefined()
~~
ts-jest[root] (WARN) The \`mocked\` helper has been moved to \`ts-jest/utils\`. Use \`import { mocked } from 'ts-jest/utils'\` instead.
PASS ./deprecated.spec.ts
Test Suites: 1 failed, 2 passed, 3 total
Tests: 3 passed, 3 total
Snapshots: 0 total
Time: XXs
Ran all test suites.
================================================================================
`;
2 changes: 1 addition & 1 deletion src/cli/cli.spec.ts
Expand Up @@ -154,7 +154,7 @@ Jest configuration written to "${normalize('/foo/bar/jest.config.foo.js')}".
expect(fs.writeFileSync.mock.calls).toEqual([
[
normalize('/foo/bar/jest.config.foo.js'),
`const tsjPreset = require('ts-jest/presets/js-with-ts');
`const { jsWithTs: tsjPreset } = require('ts-jest/presets');
module.exports = {
...tsjPreset,
Expand Down
2 changes: 1 addition & 1 deletion src/cli/helpers/presets.ts
Expand Up @@ -38,7 +38,7 @@ const definePreset = (fullName: string): TsJestPresetDescriptor => ({
return require(`../../../${fullName.replace(/^ts-jest\//, '')}/jest-preset`)
},
jsImport(varName = 'tsjPreset') {
return `const ${varName} = require('${this.fullName}')`
return `const { ${this.jsVarName}: ${varName} } = require('ts-jest/presets')`
},
get isDefault() {
return fullName === JestPresetNames.default
Expand Down

0 comments on commit 7b2dd01

Please sign in to comment.