Loosing jest variable when using esm package - jestjs

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

Related

How to update project when a dependency switches to pure ESM

We are developing a project that uses commonjs. When node-fetch is updated to v3.x, jest complains that:
import fetch, { RequestInfo, RequestInit, Response } from 'node-fetch';
SyntaxError: Cannot use import statement outside a module
npm run build succeeds.
If I use node --experimental-vm-modules node_modules/.bin/jest instead of jest, I get:
Must use import to load ES Module
I did some reading, it appears that we need to migrate our project to be an ESM as well. But it does not appear to be easy.
So the question is: when a dependency becomes pure ESM, how do we handle that in our code?
FYI, our project is: https://gitlab.com/tezgraph/tezgraph

Using es6 modules in coffeescript

es6 modules are supposed to work in coffeescript (see https://coffeescript.org/#modules), but even with an extremely simple project, it doesn't work for me. I:
Created a new directory
Ran 'npm init -y' in it
Added the key "type": "module" in my package.json
Created 2 files: index.coffee and utils.coffee
import {myprint} from 'utils.coffee'
myprint("Hello, World!")
export myprint = (str) ->
   console.log(str)
when I try to execute index.coffee (via 'coffee index.coffee' - I've tried both git's bash shell - on Windows and PowerShell), I get the following error message:
(node:1856) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
and later:
SyntaxError: Cannot use import statement outside a module
This is all correct as far as I understand. Coffeescript transpiles the import/export statements correctly, but does not provide an environment to run them.
From the documentation:
Note that the CoffeeScript compiler does not resolve modules; writing an import or export statement in CoffeeScript will produce an import or export statement in the resulting output. It is your responsibility to transpile this ES2015 syntax into code that will work in your target runtimes.
You won't be able to run this code via either coffee or node --require coffeescript/register. I believe you will have to transpile your code and run the resulting JS via node

How do I use harmony_async_iteration with jest?

I would like to test code in a node project that uses async iteration without using babel.
Based on https://github.com/facebook/jest/issues/2485, I tried running jest using node --harmony_async_iteration ./node_modules/.bin/jest. However, I still see syntax errors in my module where async iteration syntax is used. The node command line option was ineffective, as if jest spawned a new process for the test runner, without using --harmony_async_iteration.
My async iteration source file is parsed without error when loaded by node (without jest) using --harmony_async_iteration.
Babel is an integral part of jest. It is not necessary to use babel to transpile your application for non-test use, but babel absolutely must be configured in order to allow async generators to work with jest.
Install the babel-plugin-transform-async-generator-functions:
npm install --save-dev babel-plugin-transform-async-generator-functions
or
yarn add --dev babel-plugin-transform-async-generator-functions
Activate the plugin by creating a .babelrc file (or add to the file):
{ "plugins": ["transform-async-generator-functions"] }
Now your jest tests should work with async generators.

Error importing Leaflet and Leaflet-Providers when run in node

I am running unit tests on a leaflet app with tape + babel-register using es6 modules and I'm getting
ReferenceError: L is not defined
in leaflet-providers.js when importing leaflet and leaflet-providers in the module I am testing.
testfile.js
import L from 'leaflet';
import { provider } from 'leaflet-providers';
I'm not entirely sure my syntax is correct for importing a leaflet plugin using es6 modules, but it's working for me in the browser, and only not working when I run unit tests through node.
Why is leaflet-providers unable to find L when run through node?
In the browser Leaflet attaches to the window object, Node doesn't have that. In addition, since everything is a module, scope is restricted to the file level. Node does have a global object though that is a pseudo equivalent to window, with some differences.
You'll probably also need something like jsdom to provide a window object for leaflet-providers to interact with.
I use Mocha, but the following gives me no errors and attaches the providers function to L.tileLayer.providers:
script
"mocha": "mocha ./test/index.js -r jsdom-global/register
index.js
global.L = require('leaflet');
require('leaflet-providers');
My jsdom packages are jsdom and jsdom-global

How to use Jest's assertion library without using the Jest test runner?

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

Resources