aboutsummaryrefslogtreecommitdiffstats
path: root/config/jestEsbuildTransformer.js
blob: 6a4760f2546ebba4ca3f61c32290452a3cf1c2d7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import { transform } from 'esbuild';

import { node } from './buildConstants.js';

/**
 * @param {string} source
 * @param {string} filePath
 * @return {Promise<import('@jest/transform').TransformedSource>}
 */
async function processAsync(source, filePath) {
  const { code, map } = await transform(source, {
    loader: filePath.endsWith('tsx') ? 'tsx' : 'ts',
    sourcefile: filePath,
    format: 'esm',
    target: node,
    sourcemap: true,
    define: {
      __DEV__: JSON.stringify(false), // For mobx
      'process.env.NODE_ENV': 'test',
      'process.env.MODE': 'test',
      'import.meta.env': JSON.stringify({
        DEV: false,
        MODE: 'test',
        PROD: true,
      }),
    },
  });
  return {
    code,
    map,
  };
}

/** @type {import('@jest/transform').AsyncTransformer<void>} */
export default {
  canInstrument: false,
  processAsync,
};