'Define is not defined' error when running Jest test. How to mock RequireJS dependency? - jestjs

I am getting the following error when trying to write a Jest test for the following file :
Error :
Looks like this is because requireJS is not supported by Jest. How do I mock this dependency? I tried the following but it doesn't seem to work.
jest.mock('define', () => {
// mock implementation
})

Related

nestjs testing - how to start a server with jest mocked modules

I already have e2e backend tests running using jest and some mocked modules using fastify and now that when I use app.listen it will be able to receive calls.
My problem:
I would like to start the nestjs server with the mocked jest modules to be able to call it from outside without the jest scope e.g. for vulnerability scanning of the api endpoints. I have some external dependencies that I want to exclude - this is why I would like to use my jest mocked nestjs modules.
if I try to used ts-node an error will be thrown in those mocked modules
command:
npx ts-node run.ts
file: run.ts
import { initializeApplication } from './app';
(async () => {
const app = await initializeApplication();
app.listen(3005, '0.0.0.0');
})();
error:
jest.fn(
^
ReferenceError: jest is not defined
I would like to be able to start the nestjs backend with jest mocked nestjs modules to make external api calls against the backend using fake dependencies.

Vite require("global") undefinied with Jest and Storybook

I'm trying to create some image snapshots. My test file looks like this:
image-snapshots.test.ts
import initStoryshots from "#storybook/addon-storyshots";
initStoryshots({
suite: "Image snapshots",
});
However when running
jest image-snapshots.test.ts
I get an issue that globalWindow is undefined which is:
var globalWindow = _global.default.window,
var _global = _interopRequireDefault(require("global"));
So I know this is likely the case that Vite is not providing those globals, so my questions is how I could stub it so it is available for Jest?

Jest integration with Adonis V5

I'm developing an application with Adonis v5 but its test runner isn't finished. So, my workaround is to apply Jest in its place. I got this working but I'm having trouble importing types from Adonis.
In any model, I have the following import:
import { BaseModel, column } from '#ioc:Adonis/Lucid/Orm'
To solve the alias, I added this rule in jest.config.js:
moduleNameMapper: {
'^App(.*)$': '<rootDir>/app$1',
'^#ioc:Adonis/Lucid/Database$': '#adonisjs/lucid/build/src/Database/index.js',
'^#ioc:Adonis/Lucid/Orm$': '#adonisjs/lucid/build/adonis-typings/orm.d.ts',
//'^#ioc:Adonis/Core/Validator$': '',
},
The third rule points to the previous import. Inside of the pointed file, I found the declaration of a module which exports the desired types.
declare module '#ioc:Adonis/Lucid/Orm' {
import { ScopeFn, LucidModel, HooksDecorator, ...
...
The complete file is this.
When I run Jest, I get this error. What am I missing? Before I forget, I'm using ts-jest to define the settings of Jest.

mock module which does not exist?

When i run my mocha tests in my meteor app by:
node_modules/.bin/mocha --compilers js:babel-core/register //..opts
i get a problem when my module under test wants to import:
import { Meteor } from 'meteor/meteor';
So i tried to mock it with mockery:
mockery.enable();
moduleUnderTest = '../moduleUnderTest';
mockery.registerAllowable(moduleUnderTest);
meteorMock = {};
mockery.registerMock('Meteor', meteorMock);
Unfortunately the module cannot be found
Error: Cannot find module 'meteor/meteor'
So the mocking of Meteor cannot be done.
Is there a way how i can fake the location meteor/meteor?
(Alternate Solution: If i can get access to the Meteor Environment in my mocha test)
If you look at the documentation, you'll see that .registerAllowable takes a string, not a module. You also need to give the exact module name that you are mocking, and provide a fake module with the values you want.
So:
var mockery = require("mockery");
mockery.enable();
mockery.registerAllowable("./moduleUnderTest");
// We want Meteor to have the value "foo". You probably want something
// different.
var meteorMock = { Meteor: "foo" };
// We mock 'meteor/meteor' because that's what's imported.
mockery.registerMock('meteor/meteor', meteorMock);
If you think about it, what you were doing cannot work. You were requiring the module before Mockery is configured for mocking 'Meteor', so Node loads your module, and then tries to load Meteor before the mock is available, and you get a failure.
Moreover, Meteor mocks modules so when you register a mock, you have to give a module name, not the name of a variable.

How to test Angular2 pipe in nodejs with mocha without karma

I'd like to be able to test an Angular2 pipe purely in nodejs environment without including karma etc.
It is possible to use typescript files as test suites for mocha
https://templecoding.com/blog/2016/05/05/unit-testing-with-typescript-and-mocha/
But when I have a import {Pipe} from '#angular/core' it gives me
/Users/foo/node_modules/#angular/core/src/util/decorators.js:173
throw 'reflect-metadata shim is required when using class decorators';
^
reflect-metadata shim is required when using class decorators
Even if I write require('reflect-metadata') in my test file it still breaks with the same error.
Angular internally has this check:
(function checkReflect() {
if (!(Reflect && Reflect.getMetadata)) {
throw 'reflect-metadata shim is required when using class decorators';
}
})();
And after requireing reflect-matadata I indeed have Reflect on the global object, however it still doesn't work...
Anyway is there a way to test an Angular pipe purley in nodejs with mocha?
I'm using webpack to bundle the app so the file I'm requring in my test file looks like this:
import {Pipe} from '#angular/core';
#Pipe({
name: 'filterBy'
})
export class FilterByPipe {
transform(items = [], prop, val) {
return items.filter(someFilteringAlgorithm(prop, val));
}
}
I didn't test pipes yet, but here a post that explain how to test Angular 2 application in Node with Mocha. But It use Webpack instead of ts-node : Here
Hope It can help.

Resources