Skip to content

Commit

Permalink
test(preset): updates tests related to presets
Browse files Browse the repository at this point in the history
  • Loading branch information
huafu committed Oct 6, 2018
1 parent 68abcfb commit 8d5a60a
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 21 deletions.
2 changes: 1 addition & 1 deletion e2e/__cases__/deep/src/Tests/jest.config.js
@@ -1,7 +1,7 @@
const cfg = {}

if (require('jest/package.json').version.split('.').shift() === '22') {
Object.assign(cfg, require('ts-jest').jestPreset)
Object.assign(cfg, require('ts-jest/presets').defaults)
} else {
cfg.preset = 'ts-jest'
}
Expand Down
29 changes: 15 additions & 14 deletions src/cli/cli.spec.ts
@@ -1,7 +1,7 @@
import * as _fs from 'fs'
import { normalize, resolve } from 'path'

import { mocked } from '../..'
import { mocked } from '../../utils'
import { logTargetMock, mockObject, mockWriteStream } from '../__helpers__/mocks'

import { processArgv } from '.'
Expand All @@ -10,23 +10,22 @@ import { processArgv } from '.'
jest.mock('fs')

const fs = mocked(_fs)
const logTarget = logTargetMock()
let lastExitCode: number | undefined

const runCli = async (
...args: any[]
): Promise<{ stdout: string; stderr: string; exitCode: number | undefined; log: string }> => {
mockedProcess.stderr.clear()
mockedProcess.stdout.clear()
logTarget.clear()
logTargetMock().clear()
mockedProcess.argv.splice(2, mockedProcess.argv.length - 2, ...args)
lastExitCode = undefined
await processArgv()
return {
exitCode: lastExitCode,
stdout: mockedProcess.stdout.written.join('\n'),
stderr: mockedProcess.stderr.written.join('\n'),
log: logTarget.lines.join('\n'),
log: logTargetMock().lines.join('\n'),
}
}

Expand Down Expand Up @@ -55,7 +54,7 @@ beforeEach(() => {
fs.writeFileSync.mockClear()
fs.existsSync.mockClear()
fs.readFileSync.mockClear()
logTarget.clear()
logTargetMock().clear()
})
afterEach(() => {
mockedProcess.mockRestore()
Expand Down Expand Up @@ -112,12 +111,13 @@ describe('config', async () => {
const noOption = ['config:init']
const fullOptions = [
...noOption,
'--babel',
'--tsconfig',
'tsconfig.test.json',
'--jsdom',
'--no-jest-preset',
'--allow-js',
'--js',
'ts',
'--babel',
]
it('should create a jest.config.json (without options)', async () => {
expect.assertions(2)
Expand Down Expand Up @@ -154,10 +154,10 @@ 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 tsJest = require('ts-jest').createJestPreset({ allowJs: true });
`const tsjPreset = require('ts-jest/presets/js-with-ts');
module.exports = {
...tsJest,
...tsjPreset,
globals: {
'ts-jest': {
tsconfig: 'tsconfig.test.json',
Expand Down Expand Up @@ -198,8 +198,7 @@ Jest configuration written to "${normalize('/foo/bar/package.json')}".
const res = await runCli(...fullOptions, 'package.json')
expect(res).toEqual({
exitCode: 0,
log: `[level:20] creating jest presets handling JavaScript files
`,
log: '',
stderr: `
Jest configuration written to "${normalize('/foo/bar/package.json')}".
`,
Expand Down Expand Up @@ -259,10 +258,11 @@ Arguments:
Options:
--force Discard any existing Jest config
--allow-js ts-jest will be used to process JS files as well
--js ts|babel Process .js files with ts-jest if 'ts' or with
babel-jest if 'babel'
--no-jest-preset Disable the use of Jest presets
--tsconfig <file> Path to the tsconfig.json file
--babel Call BabelJest after ts-jest
--babel Pipe babel-jest after ts-jest
--jsdom Use jsdom as test environment instead of node
",
}
Expand Down Expand Up @@ -550,7 +550,8 @@ Arguments:
the \\"jest\\" property.
Options:
--allow-js ts-jest will be used to process JS files as well
--js ts|babel Process .js files with ts-jest if 'ts' or with
babel-jest if 'babel'
--no-jest-preset Disable the use of Jest presets
",
}
Expand Down
4 changes: 2 additions & 2 deletions src/cli/helpers/presets.ts
@@ -1,4 +1,4 @@
import { TsJestPresets } from '../../types'
import { TsJestPresets } from '../../config/create-jest-preset'

/** @internal */
export enum JestPresetNames {
Expand Down Expand Up @@ -38,7 +38,7 @@ const definePreset = (fullName: string): TsJestPresetDescriptor => ({
return require(`../../../${fullName.replace(/^ts-jest\//, '')}/jest-preset`)
},
jsImport(varName = 'tsjPreset') {
return `const { ${this.jsVarName}: ${varName} } = require('${this.fullName}')`
return `const ${varName} = require('${this.fullName}')`
},
get isDefault() {
return fullName === JestPresetNames.default
Expand Down
2 changes: 1 addition & 1 deletion src/cli/index.ts
Expand Up @@ -16,7 +16,7 @@ export type CliCommand = (argv: Arguments, logger: Logger) => Promise<void>
async function cli(args: string[]): Promise<void> {
const parsedArgv = yargsParser(args, {
boolean: ['dry-run', 'jest-preset', 'allow-js', 'diff', 'babel', 'force', 'jsdom'],
string: ['tsconfig'],
string: ['tsconfig', 'js'],
count: ['verbose'],
alias: { verbose: ['v'] },
default: { jestPreset: true, verbose: 0 },
Expand Down
2 changes: 1 addition & 1 deletion src/config/config-set.spec.ts
Expand Up @@ -3,7 +3,7 @@ import { resolve } from 'path'
import ts, { Diagnostic, DiagnosticCategory, ModuleKind, ScriptTarget } from 'typescript'

import * as _myModule from '..'
import { mocked } from '../..'
import { mocked } from '../../utils'
import * as fakers from '../__helpers__/fakers'
import { logTargetMock } from '../__helpers__/mocks'
import { TsJestGlobalOptions } from '../types'
Expand Down
2 changes: 1 addition & 1 deletion src/util/jsonable-value.spec.ts
@@ -1,4 +1,4 @@
import { mocked } from '../..'
import { mocked } from '../../utils'

import * as _json from './json'
import { JsonableValue } from './jsonable-value'
Expand Down
2 changes: 1 addition & 1 deletion src/util/version-checkers.spec.ts
@@ -1,5 +1,5 @@
// tslint:disable:max-line-length
import { mocked } from '../..'
import { mocked } from '../../utils'
import { logTargetMock } from '../__helpers__/mocks'

import * as _pv from './get-package-version'
Expand Down

0 comments on commit 8d5a60a

Please sign in to comment.