#javascript #jestjs
Вопрос:
Когда я запускаю тестовую команду npm в среде Jest, я получаю следующее неожиданное ключевое слово ошибки «новый», Отчет об ошибке выглядит так
● 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.
• 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:
SyntaxError: /Users/steinko/development/TutorialKubernetesClient/JestConfig/cluster.test.js: Unexpected keyword 'new'. (5:11)
3 |
4 | it('should exist a kubeconfig', {
> 5 | expect(new kubernetesClient.KubeConfig()).not.toBeNull();
| ^
6 | })
at Parser._raise (node_modules/@babel/parser/src/parser/error.js:134:45)
at Parser.raiseWithData (node_modules/@babel/parser/src/parser/error.js:129:17)
at Parser.raise (node_modules/@babel/parser/src/parser/error.js:78:17)
at Parser.checkReservedWord (node_modules/@babel/parser/src/parser/expression.js:2386:12)
at Parser.parseIdentifierName (node_modules/@babel/parser/src/parser/expression.js:2336:12)
at Parser.parseIdentifier (node_modules/@babel/parser/src/parser/expression.js:2306:23)
at Parser.parseBindingAtom (node_modules/@babel/parser/src/parser/lval.js:299:17)
at Parser.parseMaybeDefault (node_modules/@babel/parser/src/parser/lval.js:370:25)
at Parser.parseAssignableListItem (node_modules/@babel/parser/src/parser/lval.js:346:23)
at Parser.parseBindingList (node_modules/@babel/parser/src/parser/lval.js:336:24)
----------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
----------|---------|----------|---------|---------|-------------------
All files | 100 | 100 | 100 | 100 |
sum.js | 100 | 100 | 100 | 100 |
----------|---------|----------|---------|---------|-------------------
Test Suites: 1 failed, 1 passed, 2 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 1.069 s
Тест, который не прошел, выглядит так
const kubernetesClient = require('@kubernetes/client-node');
it('should exist a kubeconfig', {
expect(new kubernetesClient.KubeConfig()).not.toBeNull();
})
Я создал самую веселую обстановку с помощью:
- установка форсунок
npm install --save-dev jest
- и сгенерировал файл базовой конфигурации с помощью
jest --init
Со следующей конфигурацией
Следующие вопросы помогут Jest создать подходящую конфигурацию для вашего проекта
✔ Would you like to use Typescript for the configuration file? … no
✔ Choose the test environment that will be used for testing › jsdom (browser-like)
✔ Do you want Jest to add coverage reports? … yes
✔ Which provider should be used to instrument code for coverage? › v8
✔ Automatically clear mock calls and instances between every test? … yes
Когда я запускаю тест, возникает следующая ошибка
test jest
PASS ./sum.test.js
FAIL ./cluster.test.js
● 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.
• 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:
SyntaxError: /Users/steinko/development/TutorialKubernetesClient/JestConfig/cluster.test.js: Unexpected keyword 'new'. (5:11)
3 |
4 | it('should exist a kubeconfig', {
> 5 | expect(new kubernetesClient.KubeConfig()).not.toBeNull();
| ^
6 | })
at Parser._raise (node_modules/@babel/parser/src/parser/error.js:134:45)
at Parser.raiseWithData (node_modules/@babel/parser/src/parser/error.js:129:17)
at Parser.raise (node_modules/@babel/parser/src/parser/error.js:78:17)
at Parser.checkReservedWord (node_modules/@babel/parser/src/parser/expression.js:2386:12)
at Parser.parseIdentifierName (node_modules/@babel/parser/src/parser/expression.js:2336:12)
at Parser.parseIdentifier (node_modules/@babel/parser/src/parser/expression.js:2306:23)
at Parser.parseBindingAtom (node_modules/@babel/parser/src/parser/lval.js:299:17)
at Parser.parseMaybeDefault (node_modules/@babel/parser/src/parser/lval.js:370:25)
at Parser.parseAssignableListItem (node_modules/@babel/parser/src/parser/lval.js:346:23)
at Parser.parseBindingList (node_modules/@babel/parser/src/parser/lval.js:336:24)
----------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
----------|---------|----------|---------|---------|-------------------
All files | 100 | 100 | 100 | 100 |
sum.js | 100 | 100 | 100 | 100 |
----------|---------|----------|---------|---------|-------------------
Test Suites: 1 failed, 1 passed, 2 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 1.069 s
Что мне нужно сделать, чтобы исправить эту ошибку?
Ответ №1:
it('should exist a kubeconfig', {
должно быть
it('should exist a kubeconfig', function() {