无法使用 jest 测试从内部导入 SCSS 的 Svelte 组件 node_modules

Unable to use jest to test Svelte components which import SCSS from inside node_modules

我正在尝试将 Jest 测试添加到使用 svelte-material-ui.

的项目中

我在 GitHub 上关注这个问题,但无法让它工作:

https://github.com/hperrin/svelte-material-ui/issues/91

这是我所做的:

npx degit "sveltejs/template-webpack" smui-jest-testing
cd smui-jest-testing
yarn
yarn add svelte-material-ui
yarn add -D @testing-library/svelte
yarn add -D jest
yarn add -D svelte-jester
yarn add -D @testing-library/jest-dom
yarn add -D identity-obj-proxy
yarn add -D @babel/preset-env

我添加了一个组件Hello.svelte:

<script>
import { Button } from '@smui/button';
</script>

<Button>
Hi there.
</Button>

我这样添加src/__tests__/App.spec.js

// NOTE: jest-dom adds handy assertions to Jest and it is recommended, but not required.
import '@testing-library/jest-dom/extend-expect'

import { render,
  //fireEvent
} from '@testing-library/svelte'

import Hello from '../Hello.svelte';

test('shows proper heading when rendered', () => {
  const { getByText } = render(Hello)
  expect(getByText('Hi there')).toBeInTheDocument()
})

我的jest.config.js是这样的:

module.exports = {
  moduleFileExtensions: ['js', 'svelte'],
  moduleNameMapper: {
    '^utils(.*)$': '<rootDir>/src/utils',
    '\.(css|less|scss)$': 'identity-obj-proxy',
  },
  transformIgnorePatterns: ['node_modules/(?!(@smui)/)'],
  testPathIgnorePatterns: ['/node_modules/', '/cypress/'],
  setupFilesAfterEnv: [
    // '<rootDir>/jest.setup.js',
    '@testing-library/jest-dom/extend-expect',
  ],
  transform: {
    // '^.+\.tsx?$': 'ts-jest',
    '^.+\.js$': 'babel-jest',
    '^.+\.svelte$': ['svelte-jester', { preprocess: false }],
  },
  collectCoverageFrom: [
    '!./src/client.js',
    '!./src/server.js',
    '!./src/service-worker.js',
    './src/**/*.svelte',
    './src/**/*.ts',
    './src/**/*.js',
  ],
};

我的package.json最后是这样的:

{
  "name": "svelte-app",
  "version": "1.0.0",
  "devDependencies": {
    "@babel/preset-env": "^7.8.7",
    "@testing-library/jest-dom": "^5.1.1",
    "@testing-library/svelte": "^1.11.0",
    "cross-env": "^5.2.0",
    "css-loader": "^2.1.1",
    "identity-obj-proxy": "^3.0.0",
    "jest": "^25.1.0",
    "mini-css-extract-plugin": "^0.6.0",
    "serve": "^11.0.0",
    "style-loader": "^0.23.1",
    "svelte": "^3.0.0",
    "svelte-jester": "^1.0.5",
    "svelte-loader": "2.13.3",
    "webpack": "^4.30.0",
    "webpack-cli": "^3.3.0",
    "webpack-dev-server": "^3.3.1"
  },
  "scripts": {
    "build": "cross-env NODE_ENV=production webpack",
    "dev": "webpack-dev-server --content-base public",
    "test": "jest src",
    "test:watch": "npm run test -- --watch"
  },
  "jest": {
    "transform": {
      "^.+\.svelte$": "svelte-jester"
    },
    "moduleFileExtensions": [
      "js",
      "svelte"
    ],
    "setupFilesAfterEnv": [
      "@testing-library/jest-dom/extend-expect"
    ]
  },
  "dependencies": {
    "svelte-material-ui": "^1.0.0-beta.20"
  }
}

我有一个 .babelrc.json 里面有这个:

{
  "presets": [["@babel/preset-env", { "targets": { "node": "current" } }]]
}

当我 运行 yarn test 我看到这个:

$ yarn test
yarn run v1.19.1
warning package.json: No license field
$ jest src
 FAIL  src/__tests__/App.spec.js
  ● Test suite failed to run

    Jest encountered an unexpected token

    This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript.

    By default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules".

    Here's what you can do:
     • 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/en/configuration.html

    Details:

    /home/chrisdawson/Projects/.../javascript-overlay/smui-jest-testing/node_modules/@smui/button/index.js:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import './_index.scss';
                                                                                             ^^^^^^

    SyntaxError: Cannot use import statement outside a module

      1 | <script>
      2 | 
    > 3 | import { Button } from '@smui/button';
        |                ^
      4 | </script>
      5 | 
      6 | <Button>

      at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1059:14)
      at Object.<anonymous> (src/Hello.svelte:3:16)

Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        0.941s
Ran all test suites matching /src/i.
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

这里有什么特别的吗?

我对 Jest、Babel 等一窍不通,所以不要问我为什么,但你应该将你的 .babelrc.json 重命名为 babel.config.js,并从此文件中导出你的配置:

module.exports = {
  presets: [['@babel/preset-env', { targets: { node: 'current' } }]],
}

您的错误消息看起来像是来自 smui 的文件,在 node_modules 下,没有被转译(因此在 iife 中间以 import 结尾,即使在 ES6 中也是被禁止的),所以我发现 this comment 提供了解决方案。

接下来,Button@smui/button的默认导出,所以改成:

import { Button } from '@smui/button';

至:

import Button from '@smui/button';

这个,我相信你会自己找到的。

所以看到绿色标记前的最后一个,我留给你找;)