Skip to content

Commit

Permalink
fix: inlineSnapshots when running trough babel-jest
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergio Moreno committed May 7, 2021
1 parent 5a00c18 commit 25e9b64
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 18 deletions.
33 changes: 17 additions & 16 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,37 @@
import { extname } from 'path'

import { Config } from '@jest/types'
import { TransformOptions as JestTransformOptions, Transformer } from '@jest/transform'
import { Format, Loader, TransformOptions, transformSync } from 'esbuild'

import { Options } from './options'
import type { TransformOptions as JestTransformOptions, Transformer } from '@jest/transform'
import type { Config } from '@jest/types'
import type { Options } from './options'

import { getExt, loaders } from './utils'

const createTransformer = (options?: Options) => ({
process(content: string,
filename: string,
config: Config.ProjectConfig,
process(content: string,
filename: string,
config: Config.ProjectConfig,
opts?: JestTransformOptions
) {
const sources = { code: content }
const ext = getExt(filename), extName = extname(filename).slice(1)

const enableSourcemaps = options?.sourcemap || false
const loader = (options?.loaders && options?.loaders[ext]
// Caution: disabling this, can cause issues with inlineSnapshots since can't find the original line
const enableSourcemaps = options?.sourcemap || true
const loader = (options?.loaders && options?.loaders[ext]
? options.loaders[ext]
: loaders.includes(extName) ? extName: 'text'
) as Loader
const sourcemaps: Partial<TransformOptions> = enableSourcemaps
? { sourcemap: true, sourcesContent: false, sourcefile: filename }
const sourcemaps: Partial<TransformOptions> = enableSourcemaps
? { sourcemap: true, sourcesContent: false, sourcefile: filename }
: {}

/// this logic or code from
/// this logic or code from
/// https://github.com/threepointone/esjest-transform/blob/main/src/index.js
/// this will support the jest.mock
/// https://github.com/aelbore/esbuild-jest/issues/12
/// TODO: transform the jest.mock to a function using babel traverse/parse then hoist it
if (sources.code.indexOf("ock(") >= 0 || opts?.instrument) {
if (sources.code.indexOf("jest.mock(") >= 0 || opts?.instrument) {
const source = require('./transformer').babelTransform({
sourceText: content,
sourcePath: filename,
Expand All @@ -48,21 +49,21 @@ const createTransformer = (options?: Options) => ({
...(options?.jsxFragment ? { jsxFragment: options.jsxFragment }: {}),
...sourcemaps
})

let { map, code } = result;
if (enableSourcemaps) {
map = {
...JSON.parse(result.map),
sourcesContent: null,
}

// Append the inline sourcemap manually to ensure the "sourcesContent"
// is null. Otherwise, breakpoints won't pause within the actual source.
code = code + '\n//# sourceMappingURL=data:application/json;base64,' + Buffer.from(JSON.stringify(map)).toString('base64')
} else {
map = null
}

return { code, map }
}
})
Expand Down
2 changes: 1 addition & 1 deletion src/options.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Loader } from 'esbuild'
import type { Loader } from 'esbuild'

export interface Options {
jsxFactory?: string
Expand Down
2 changes: 1 addition & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import path from 'path'
import path from 'path'

export const loaders = ["js", "jsx", "ts", "tsx", "json"]

Expand Down

0 comments on commit 25e9b64

Please sign in to comment.