I'm trying to follow the advice in https://jestjs.io/docs/en/api about importing globals, namely this:
However, if you prefer explicit imports, you can do import {describe, expect, it} from '#jest/globals'.
It doesn't work, though. I added the import at the top of my two test files and when I run jest, both test suites fail with the message "Do not import #jest/globals outside of the Jest test environment."
I also have jest configured in eslint env, in case it makes a difference.
Can someone point me in the right direction, please?
I just found out what the problem was: I was importing the #jest/globals package in a helper file which isn't a test file. Removing the import from there and leaving it only in the two test files allows Jest to run without a problem.
Related
I'm having some issues including a library in a nim test file.
My code in my test file looks like this.
import unittest
import testToolpkg/Login
import dotenv
load()
test "valid login cookies":
var cookies = loginCookies(os.get("EMAIL"), os.get("PASSWORD"), os.get("URL"))
check cookies.len != 0
However when I run nimble test I get this error Error: cannot open file: dotenv. This seems to be an issue with any external library I try to use in a test.
The dotenv library works fine outside of the tests folder. I'm not sure if I'm missing some kind of option to get this to work? I am running this on windows as well.
After adding the lib to my .nimble file
requires "dotenv >= 2.0.0"
The issue was resolved!
I followed this answer to make jest work with esm packages
node - using jest with esm package
But I get an error when I try to do jest.SpyOn because it said that jest is undefined.
I did some digging and notice that jest is present on my app.test.js file but not in the asserts.js
So I was wondering how can I get the jest variable on the assert file or can I import jest directly without relaying on injecting it like the default config
I am trying to learn Node and React and I ran into an interesting problem where - in the import statement like below, I need to explicitly type the file format (.jsx) otherwise, the compiler borks up and complains that it cannot find App.js file.
import App from './App.jsx';
Note - App is a jsx file and saved as App.jsx. I used create-react-app for boilerplate code.
Also, I found some more information on GitHub on the lines of "The distinction between .js and .jsx files was useful before Babel, but it’s not that useful anymore."
https://github.com/facebook/create-react-app/issues/87
So, it looks like a non-issue as long as save it as .js and I have babel to compile ES6.. Thanks everyone!
Here your assumption is incorrect.
If I am right then you are assuming that if your file is .jsx, then you don't need to specify the file extension in the import statement to import .jsx file.
But its the other way round.
If you don't specify the extension of the file then compiler assumes that it is a .js file. So, there is nothing wrong with the behavior that you are seeing.
OR
If you don't want to include extensions in the import statement then just create .js files. Compiler will automatically detect the jsx written inside it. This is the easiest way to fool import statement.
Any javascript react snippets will not show up in a plain .js file only a .jsx file
I suggest that closing the running project on Browser
(ctrl + c / command + c on terminal to finish running project) and do
'yarn start' or 'npm start' again. then it would work!
React sometimes shows errors when you add new files like jsx, js, etc..
and I also had the same problem with you.
(I changed App.js to App.jsx and webppack error was occured).
In these case re-starting project would be the solution!
finally you don't need to specify file extension (like the other answers suggestion).
Jest creates this global called expect. How can I use that without using their test runner? i.e., what do I have to import/require?
The module is simply called expect
e.g.
import expect from 'expect'
expect(1 + 1).toBe(2)
You can see it in the Jest lerna repo and the individual package on npm
I'm working on a node app that uses mocha to run unit tests.
When I run this command:
mocha --compilers coffee:coffee-script --reporter spec ./test/unit/*-test.coffee
I get this error:
ERROR: Unknown option --compilers
It seems mocha is confused, because it definitely has a compilers option. This error started happening when I added a new file to the project. It's the only output I can get mocha to generate. --debug does nothing.
Let's say I have a package called person installed. I want to configure this package globally so that I can import the configured object anywhere in my project. To do that, I import person, configure it as a driver, and then export it again.
However, when I import it (either in Car.coffee or Car-test.coffee), mocha fails with the above error.
Driver.coffee
driver = require 'person'
driver.setSkill "Drive"
module.exports = driver
Car.coffee
driver = require './driver'
...
Car-test.coffee
driver = require '../../src/driver'
...
Note that this works fine if I'm just compiling with coffee and running the node project. There's no issue importing it there. But when I run with mocha, it fails if I import the file.
I can't really pinpoint the error. It seems just like a bug in mocha, but maybe I'm doing something "bad" by exporting the same object that I import, and node is just more forgiving?
I'm using the latest version of mocha (1.13.0). Thanks!
Edit:
This doesn't fix the error, and is not ideal syntax-wise:
person = require 'person'
class driver
constructor: ->
person.setSkill "Drive"
#person = person
module.exports = driver
Note that simply wrapping it in a plain object doesn't work.
Edit 2:
Here's something else that doesn't work:
configure-driver.coffee
configureDriver = (person) ->
person.setSkill "Drive"
module.exports = configureDriver
car.coffee
driver = require('./configure-driver')(require 'person')
Mocha throws the same error as before.
Maybe a little late but hopefully it will help someone (I just spent an hour paging through mocha's source code to track this down).
Try that command instead (the important bit is the equal sign after --compilers):
mocha --compilers=coffee:coffee-script --reporter spec ./test/unit/*-test.coffee
I ran into that bug while trying to create a new grunt test taks using grunt-mocha-istanbul and coffeescript test definitions. Strangely, if I ran the command directly in my shell it worked but using the grunt task I got the same error as you did.
It seems Mocha uses commander and it's global. In my case I had a script under the test directory that uses commander. It looks like Mocha executes the test scripts, parses mocha.opts, and then executes the specs. To fix it I just moved the scripts using commander out of the test dir and all was good.