首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法使用jest测试从node_modules内部导入SCSS的Svelte组件

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

Stack Overflow用户
提问于 2020-03-11 00:47:09
回答 1查看 1.6K关注 0票数 0

我正在尝试将Jest测试添加到使用svelte-material-ui的项目中。

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

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

下面是我所做的:

代码语言:javascript
复制
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

代码语言:javascript
复制
<script>
import { Button } from '@smui/button';
</script>

<Button>
Hi there.
</Button>

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

代码语言:javascript
复制
// 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是这样的:

代码语言:javascript
复制
module.exports = {
  moduleFileExtensions: ['js', 'svelte'],
  moduleNameMapper: {
    '^utils(.*)$': '<rootDir>/src/utils$1',
    '\\.(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最后看起来像这样:

代码语言:javascript
复制
{
  "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

代码语言:javascript
复制
{
  "presets": [["@babel/preset-env", { "targets": { "node": "current" } }]]
}

当我运行yarn test时,我看到以下内容:

代码语言:javascript
复制
$ 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.

这里有什么特别的吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-03-11 06:01:48

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

代码语言:javascript
复制
module.exports = {
  presets: [['@babel/preset-env', { targets: { node: 'current' } }]],
}

你的错误消息看起来像是来自smui的文件,在node_modules下,没有被转译(因此以生命周期中间的import结尾,这在ES6中是被禁止的),所以我发现this comment提供了一个解决方案。

接下来,Button@smui/button的默认导出,因此请更改:

代码语言:javascript
复制
import { Button } from '@smui/button';

至:

代码语言:javascript
复制
import Button from '@smui/button';

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

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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60622265

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档