How do I resolve Jest Mock error "Cannot find module" - jestjs

Jest is throwing an error stating it cannot find the module specified by path in jest.mock()
> jest
FAIL test/userSelect.test.ts
● Test suite failed to run
Cannot find module '../src/actions/selectUserById' from 'test/userSelect.test.ts'
8 |
9 | jest.mock('mysql');
> 10 | jest.mock('../src/actions/selectUserById');
11 |
12 | const mockSelectUserById = selectUserById as jest.MockedFunction<
13 | typeof selectUserById
at Resolver.resolveModule (node_modules/jest-resolve/build/index.js:306:11)
00:00
at Object.mock (test/userSelect.test.ts:10:6)
The strangest part is this:
this test passes on my local machine, but it fails in the CI service we are using.

In case anyone else is troubleshooting a similar issue,
I finally realized the root-cause of the failing test was a difference in case-sensitivity of the specified path.
In my case the true filename is src/actions/selectUserByID (notice the uppercase letter 'D'),
but the string I passed to Jest.mock() is selectUserById.
At some point, I renamed the file locally, which is why the test passes as expected on my machine. However, I learned a case-sensitive change to a filename is not detected as a change by git, which is why the CI service still had the erroneous filename.
I used git mv to commit the case-sensitive filename, and this resolved my issue.

Related

Setup unit (jest) test shopware-docker shopware 6

I try to setup the test environment for unit (jest) testing for my custom plugin vue files in Shopware6 using shopware-docker
The provided documentation is, at least for me, not really helpful. following a list of unclear hints:
What means Module: base vs Module: classic vs Module: platform? Which one i have installed? Where are a useful documentation about? The folder structure does not provide named modules
Like when the target project is Shopware 5 Git installation, to include base + classic or on Shopware 6, base + platform.
I started with Shopware 6, so i confused about this nexus
Trying command swdc admin-jest <project-name> inside Module: platform, the only which appointed the administration, returns:
rammi#rammi-laptop-i7:~/Code/my-project$ swdc admin-jest my-project
Installing Node v12.22.12 (x64)
warning: Version already installed at "/nvm/.debian-fnm/node-versions/v12.22.12"
Using Node v12.22.12
Writing definition to file ...
Done!
Using Node for alias lts-gallium
> administration#1.0.0 unit-watch
> jest --config jest.config.js --watch
No tests found related to files changed since last commit.
Press `a` to run all tests, or run Jest with `--watchAll`.
----------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
----------|---------|----------|---------|---------|-------------------
All files | 0 | 0 | 0 | 0 |
----------|---------|----------|---------|---------|-------------------
Watch Usage
› Press a to run all tests.
› Press f to run only failed tests.
› Press p to filter by a filename regex pattern.
› Press t to filter by a test name regex pattern.
› Press q to quit watch mode.
› Press Enter to trigger a test run.
Thats all..., but i think, there must be a environment configuration to achieve tests, but i dont find nothing in the official documentations. Also using google without success.
What i found is the command composer run admin:create:test from this documentation. But the command does not exist in shopware-docker repository. And a similar command is not available.
Summary: How to setup jest for custom plugin using shopware-docker?
Edit #1
I have already created a simple test like this
// custom/plugins/MyPlugin/src/Resources/app/administration/test/module/my-module/view/module-view.spec.js
describe('module-view', () => {
it('should be false', () => {
expect(false).toBeTruthy()
})
})
Using the provided command Press p to filter by a filename regex pattern. and putting module-view response with
Watch Usage: Press w to show more.
Active Filters: filename /module-view/
Pattern Mode Usage
› Press Esc to exit pattern mode.
› Press Enter to filter by a filenames regex pattern.
pattern › module-view.spec.js
No tests found, exiting with code 0
----------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
----------|---------|----------|---------|---------|-------------------
All files | 0 | 0 | 0 | 0 |
----------|---------|----------|---------|---------|-------------------
Watch Usage: Press w to show more.
As i can't use the recommend way by shopware, this is my current solution:
create folder test/e2e in ~/%my_plugin%/src/Resources/app/administration/
go to folder e2e
run npm install cypress --save-dev
run to start cypress node_modules/.bin/cypress open
BTW: the current solution cover e2e tests, but cypress also offer solution for component tests, which can used for unit test

How to fix "SyntaxError: Expected "#", "\n", "\r" or [ \t] but "[" found." which is retrieved by yargs module?

I'm currently doing continuous deployment with bitbucket pipelines. I've put in a pipe the "npm test" command because I use mocha to do unit tests on my node project.
When I run "npm test" on my local shell to test my routes, each of them works. But when the temporary server of bitbucket run the command, I retrieve this mistake :
stork#1.0.0 test /opt/atlassian/pipelines/agent/build
> mocha tests/**/*.js
/opt/atlassian/pipelines/agent/build/node_modules/mocha/node_modules/yargs/yargs.js:1163
else throw err
^
SyntaxError: Expected "#", "\n", "\r" or [ \t] but "[" found.
npm ERR! Test failed. See above for more details.
I've recently implemented a config.toml file in my repository to put in my code some variable (like domain, path, etc). Since this moment, the unit tests which are run in the pipleline doesn't work anymore.
Is it an issue of toml parsing ?
this is my toml file :
title = "configuration"
[charlie]
domain = "localhost"
path = "/"
port = 3000
Any tips should be really appreciated.
After some researches, I've found the origin of the issue.
My toml file was mentioned in the gitignore file to be not put it in my remote repository.
That's why I had to copie/past the entiere toml content to put it in a single bitbucket environment variable. With this technic, I was sure to have a prod version of the config.toml on my server and a dev version of the config.toml on my machine.
The problem was my BitBucket variable has been formatted on a single line. Indeed, the toml format is line break sensitive. That's why I've used a json instead of a toml and the issue has been fixed.

Jest Deleting snapshots when updating single test

Im having a weird issue where Jest is deleting all snapshots when I am attempting to update a single test.
if I have a directory:
07/03/2018 11:05 AM 131,285 p-Error.ts.snap
07/03/2018 11:05 AM 75,741 p-Lot.ts.snap
06/29/2018 03:39 PM 134,879 p-Split.ts.snap
and I run:
npm test -- -i -u -t="p-Split"
Here is the console output:
PASS src/__tests__/p-Split.ts (279.875s)r\openvrpaths.vrpath
FAIL src/__tests__/p-Error.ts
● Test suite failed to run
Your test suite must contain at least one test.
at node_modules/jest-cli/build/test_scheduler.js:245:22
FAIL src/__tests__/p-Lot.ts
● Test suite failed to run
Your test suite must contain at least one test.
at node_modules/jest-cli/build/test_scheduler.js:245:22
and the directory now contains:
06/29/2018 03:40 PM 134,879 p-Split.ts.snap
Thanks, Joe
Snapshots are deleted because of the -u flag, which automatically removes unused/obsolete snapshots.
Why would you want to keep those snapshots, if you removed everything from the test files p-Error and p-Lot? If you removed p-Error and p-Lot test cases by accident, bring them back.

Gitlab CI variables returns empty string?

It's been 2 days since one of my project' build starts failing on Gitlab CI. The main error was E_MISSING_APP_KEY and when I check another variable just by echoing $HOST and $PORT from my .gitlab-ci.yml config, like this
tests:
script:
- echo "${HOST} ${PORT}"
- node -e "console.log(process.env.HOST, process.env.PORT)"
- node_modules/.bin/nyc node ace test -t 0
I got nothing.
The build was failed because it can't read my environment variable that I set on its CI Settings.
Anyone experiencing same issue? & how to solve this?
Update:
I'm trying to create new project with only containing .gitlab-ci.yml file here and it's seems working just fine
But why the world it's still failing on my main project?
For anyone else having a similar problem:
check your variable, if it is protected your branch has to be protected as well or remove the protected option on your variable
The issue is solved by delete all of my variables I've had & set them back from the CI Setting. And the build pipeline is running without any errors. (except the actual testing is still failed, lol)
Honestly, I'm still wondering why this could happened? and hopefully no one will experiencing same kind of issue like me here..

Pintos, make check failed. Run didn't start up properly: no "Pintos booting" message

I found that someone has asked a relevant question before PintOS, kernel panic with -v option bochs on ubuntu
However, I tried but it didn't work. "pintos -- run alarm-multiple" seems fine but when I do "make check"
......
Run didn't start up properly: no "Pintos booting" message
pintos -v -k -T 480 --bochs -- -q -mlfqs run mlfqs-block < /dev/null 2> tests/threads/mlfqs-block.errors > tests/threads/mlfqs-block.output
perl -I../.. ../../tests/threads/mlfqs-block.ck tests/threads/mlfqs-block tests/threads/mlfqs-block.result
FAIL tests/threads/mlfqs-block
Run didn't start up properly: no "Pintos booting" message
FAIL tests/threads/alarm-single
FAIL tests/threads/alarm-multiple
FAIL tests/threads/alarm-simultaneous
FAIL tests/threads/alarm-priority
FAIL tests/threads/alarm-zero
FAIL tests/threads/alarm-negative
FAIL tests/threads/priority-change
FAIL tests/threads/priority-donate-one
FAIL tests/threads/priority-donate-multiple
FAIL tests/threads/priority-donate-multiple2
FAIL tests/threads/priority-donate-nest
FAIL tests/threads/priority-donate-sema
FAIL tests/threads/priority-donate-lower
FAIL tests/threads/priority-fifo
FAIL tests/threads/priority-preempt
FAIL tests/threads/priority-sema
FAIL tests/threads/priority-condvar
FAIL tests/threads/priority-donate-chain
FAIL tests/threads/mlfqs-load-1
FAIL tests/threads/mlfqs-load-60
FAIL tests/threads/mlfqs-load-avg
FAIL tests/threads/mlfqs-recent-1
FAIL tests/threads/mlfqs-fair-2
FAIL tests/threads/mlfqs-fair-20
FAIL tests/threads/mlfqs-nice-2
FAIL tests/threads/mlfqs-nice-10
FAIL tests/threads/mlfqs-block
27 of 27 tests failed.
../../tests/Make.tests:26: recipe for target 'check' failed
make: *** [check] Error 1
I had the same problem today and it was because i was trying to set qemu as the default simulator so I changed line 103 in utils/pintos to
$sim = "qemu" if !defined $sim;
but i forget to change the SIMULATOR value in threads/Make.vars to
SIMULATOR = --qemu
Since I didn't setup bochs on my machine make check was trying to run the tests on it but if fails to boot.
Note that this is one scenario why tests fail to run, it could be another reason but since
pintos -- run alarm-multiple
is working fine, I think this might be the same problem you have.

Resources