iRSSの日記

はてなダイアリーiRSSの日記の続き

firebase-functions-testのテストが通らない時

事象

github.com

Cloud Firestore セキュリティ ルールをfirebase-functions-testの unit testを jestで走らせようとしたら Cannot use import statement outside a module というエラーがでた。

 FAIL  src/__test__/rules/firestore.rules.spec.ts
  ● Test suite failed to run

    Jest encountered an unexpected token

    Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax, or when Jest is not configured to support such syntax.

    Out of the box Jest supports Babel, which will be used to transform your files into valid JS based on your Babel configuration.

    By default "node_modules" folder is ignored by transformers.

    Here's what you can do:
     • If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/ecmascript-modules for how to enable it.
     • If you are trying to use TypeScript, see https://jestjs.io/docs/getting-started#using-typescript
     • To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
     • If you need a custom transformation specify a "transform" option in your config.
     • If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.

    You'll find more details and examples of these config options in the docs:
    https://jestjs.io/docs/configuration
    For information about custom transformations, see:
    https://jestjs.io/docs/code-transformation

    Details:

    /Users/takaofunami/src/aid/firesync/fire2pg/functions/node_modules/firebase/compat/database/dist/index.esm.js:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){import '@firebase/database-compat';
                                                                                      ^^^^^^

    SyntaxError: Cannot use import statement outside a module

      at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1728:14)
      at Object.<anonymous> (node_modules/@firebase/rules-unit-testing/dist/index.cjs.js:5:1)

Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        0.248 s, estimated 1 s
Ran all test suites related to changed files.

原因の推定

jest-node-exports-resolverのパス推測がよろしくない模様。jest-node-exports-resolveを外して、defaultResolverを使えば、動いた

/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */

module.exports = {
  testEnvironment: "node",
  moduleNameMapper: {
    "^src/(.*)$": "<rootDir>/src/$1",
    "^@/(.*)$": "<rootDir>/src/$1",
  },
  verbose: true,
  testRegex: "(/__tests__/.*|(\\.|/)(test|spec))\\.[jt]sx?$",
  resolver: "jest-node-exports-resolver",
  modulePaths: ["node_modules"],
};

解決策

特定のモジュールだけ、jest-node-exports-resolverではなくてdefaultResolverにして、解決できた

jest.condif.ts

/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */

module.exports = {
  testEnvironment: "node",
  moduleNameMapper: {
    "^src/(.*)$": "<rootDir>/src/$1",
    "^@/(.*)$": "<rootDir>/src/$1",
  },
  verbose: true,
  testRegex: "(/__tests__/.*|(\\.|/)(test|spec))\\.[jt]sx?$",
  resolver: "<rootDir>/resolver.js",
  modulePaths: ["node_modules"],
};

resolver.js

const jest_node_exports_resolver = require("jest-node-exports-resolver");

module.exports = (request, options) => {
  if (request.match(/^(@firebase|firebase)\//)) {
    // qconsole.log({ request, options });
    return options.defaultResolver(request, options);
  } else return jest_node_exports_resolver(request, options);
};