Skip to content

Commit

Permalink
feat(experimental-esm): adds option for esm output
Browse files Browse the repository at this point in the history
  • Loading branch information
hedefalk committed Jun 4, 2020
1 parent f3ac5b0 commit f488ab6
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 8 deletions.
Empty file modified cli.js
100644 → 100755
Empty file.
1 change: 1 addition & 0 deletions src/__helpers__/fakers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export function tsJestConfig(options?: Partial<TsJestConfig>): TsJestConfig {
packageJson: undefined,
stringifyContentPathRegex: undefined,
diagnostics: { ignoreCodes: [], pretty: false, throws: true },
experimentalEsm: false,
...options,
}
}
Expand Down
15 changes: 11 additions & 4 deletions src/compiler/instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Logger } from 'bs-logger'
import { readFileSync } from 'fs'
import mkdirp = require('mkdirp')
import { basename, extname } from 'path'
import { ModuleKind } from 'typescript'

import { ConfigSet } from '../config/config-set'
import { CompileFn, CompilerInstance, MemoryCache, TSFile, TsCompiler } from '../types'
Expand Down Expand Up @@ -103,10 +104,16 @@ export const createCompilerInstance = (configs: ConfigSet): TsCompiler => {
/**
* Get the extension for a transpiled file.
*/
const getExtension =
compilerOptions.jsx === ts.JsxEmit.Preserve
? (path: string) => (/\.[tj]sx$/.test(path) ? '.jsx' : '.js')
: (_: string) => '.js'
const getExtension = (path: string) => {
const jsExtension = configs.tsJest.experimentalEsm && compilerOptions.module === ModuleKind.ESNext ? '.mjs' : '.js'

if (compilerOptions.jsx === ts.JsxEmit.Preserve) {
return /\.[tj]sx$/.test(path) ? '.jsx' : jsExtension
} else {
return jsExtension
}
}

let compilerInstance: CompilerInstance
if (!tsJest.isolatedModules) {
// Use language services by default
Expand Down
4 changes: 3 additions & 1 deletion src/config/__snapshots__/config-set.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`cacheKey should be a string 1`] = `"{\\"digest\\":\\"a0d51ca854194df8191d0e65c0ca4730f510f332\\",\\"jest\\":{\\"__backported\\":true,\\"globals\\":{}},\\"projectDepVersions\\":{\\"dev\\":\\"1.2.5\\",\\"opt\\":\\"1.2.3\\",\\"peer\\":\\"1.2.4\\",\\"std\\":\\"1.2.6\\"},\\"transformers\\":[\\"hoisting-jest-mock@1\\"],\\"tsJest\\":{\\"compiler\\":\\"typescript\\",\\"diagnostics\\":{\\"ignoreCodes\\":[6059,18002,18003],\\"pretty\\":true,\\"throws\\":true},\\"isolatedModules\\":false,\\"packageJson\\":{\\"kind\\":\\"file\\"},\\"transformers\\":[]},\\"tsconfig\\":{\\"declaration\\":false,\\"inlineSourceMap\\":false,\\"inlineSources\\":true,\\"module\\":1,\\"noEmit\\":false,\\"removeComments\\":false,\\"sourceMap\\":true,\\"target\\":1}}"`;
exports[`cacheKey should be a string 1`] = `"{\\"digest\\":\\"a0d51ca854194df8191d0e65c0ca4730f510f332\\",\\"jest\\":{\\"__backported\\":true,\\"globals\\":{}},\\"projectDepVersions\\":{\\"dev\\":\\"1.2.5\\",\\"opt\\":\\"1.2.3\\",\\"peer\\":\\"1.2.4\\",\\"std\\":\\"1.2.6\\"},\\"transformers\\":[\\"hoisting-jest-mock@1\\"],\\"tsJest\\":{\\"compiler\\":\\"typescript\\",\\"diagnostics\\":{\\"ignoreCodes\\":[6059,18002,18003],\\"pretty\\":true,\\"throws\\":true},\\"experimentalEsm\\":false,\\"isolatedModules\\":false,\\"packageJson\\":{\\"kind\\":\\"file\\"},\\"transformers\\":[]},\\"tsconfig\\":{\\"declaration\\":false,\\"inlineSourceMap\\":false,\\"inlineSources\\":true,\\"module\\":1,\\"noEmit\\":false,\\"removeComments\\":false,\\"sourceMap\\":true,\\"target\\":1}}"`;

exports[`jest should merge parent config if any with globals is an empty object 1`] = `
Object {
Expand Down Expand Up @@ -53,6 +53,7 @@ Object {
"pretty": true,
"throws": true,
},
"experimentalEsm": false,
"isolatedModules": false,
"packageJson": Object {
"kind": "file",
Expand Down Expand Up @@ -167,6 +168,7 @@ Object {
"pretty": true,
"throws": true,
},
"experimentalEsm": false,
"isolatedModules": false,
"packageJson": Object {
"kind": "file",
Expand Down
6 changes: 3 additions & 3 deletions src/config/config-set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,7 @@ export class ConfigSet {
compiler: options.compiler ?? 'typescript',
transformers,
stringifyContentPathRegex,
experimentalEsm: !!options.experimentalEsm,
}
this.logger.debug({ tsJestConfig: res }, 'normalized ts-jest config')

Expand Down Expand Up @@ -617,9 +618,8 @@ export class ConfigSet {
sourceRoot: undefined,
tsBuildInfoFile: undefined,
}
// force the module kind if not piping babel-jest
if (!this.tsJest.babelConfig) {
// commonjs is required for jest
// force the module kind if not piping babel-jest or using experimental esm feature
if (!this.tsJest.experimentalEsm && !this.tsJest.babelConfig) {
options.module = this.compilerModule.ModuleKind.CommonJS
}

Expand Down
9 changes: 9 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,14 @@ export interface TsJestGlobalOptions {
* exporting the content of the file as a string
*/
stringifyContentPathRegex?: string | RegExp

/**
* Output ecmascript modules. This will for now output files with .mjs extension to
* ensure jest treats them as modules.
*
* @default undefined (disabled)
*/
experimentalEsm?: boolean
}

interface TsJestConfig$tsConfig$file {
Expand Down Expand Up @@ -183,6 +191,7 @@ export interface TsJestConfig {
diagnostics: TsJestConfig$diagnostics
babelConfig: TsJestConfig$babelConfig
transformers: string[]
experimentalEsm: boolean
// to deprecate / deprecated === === ===
stringifyContentPathRegex: TsJestConfig$stringifyContentPathRegex
}
Expand Down

0 comments on commit f488ab6

Please sign in to comment.