mocha

NODE_ENV with Jest

ⅰ亾dé卋堺 提交于 2020-06-17 02:04:29
问题 I am migrating from Mocha to Jest. My test import the 'config' package, which selects a configuration file or another depending on the NODE_ENV variable. However, it looks like NODE_ENV variable is found by config while running the test from Jest Next line does not work (that is, NODE_ENV is ignored): NODE_ENV=test jest test/*.js --notify --config jest.config.json As a consequence the 'config' package reports: console.error node_modules/config/lib/config.js:1727 WARNING: NODE_ENV value of

Using await / async with mocha, chai

非 Y 不嫁゛ 提交于 2020-06-16 02:25:30
问题 I'm quite new to node and express. And have been trying to write test code using mocha, chai and chai-http. Here's the part of source code. const mongoose = require('mongoose'), User = require('../../models/user'); const mongoUrl = 'mongodb://xxxxxxxxxxx'; describe('/test', function() { before('connect', function() { return mongoose.createConnection(mongoUrl); }); beforeEach(async function(done) { try { await User.remove({}); // <-- This doesn't work chai.request('http://localhost:3000')

how to mock configurable middleware in node js with sinon and mocha

纵饮孤独 提交于 2020-06-13 09:07:48
问题 I have configurable middleware where I can pass parameters and based on that it calls next function. middleware code: File: my-middleware.js exports.authUser = function (options) { return function (req, res, next) { // Implement the middleware function based on the options object next() } } var mw = require('./my-middleware.js') app.use(mw.authUser({ option1: '1', option2: '2' })) How to mock the middleware using sinon js? I have done in this way but, it throwing me "TypeError: next is not a

How to transpile node_modules folder using just babel 7?

99封情书 提交于 2020-06-12 06:36:27
问题 Trying to unit test reactjs application using mocha but getting error from es6 features (import/export) used inside node_modules folder. The application in question is transpiled using babel but since one of react component is using a module from node_modules its throwing error Syntax Error:Unexpected token export . I am aware that babel ignores node_modules folder by default, but not sure how to approach this. Any help would be appreciated. Thanks. Test Command :- "test": "SET NODE_ENV=test&

Mocha + TypeScript: Cannot use import statement outside a module

旧时模样 提交于 2020-05-30 08:13:42
问题 I was watching this video in order to learn how to add some simple tests to my Express routes but I am getting all kind of errors while executing a test. The error is: import * as chai from 'chai'; ^^^^^^ SyntaxError: Cannot use import statement outside a module I have read some similar Stack Overflow questions and GitHub issues but I didn't find a solution for my own application. Finally I found Mocha documentation on GitHub regarding ES modules but it didn't work: I created the app using

Mocha + TypeScript: Cannot use import statement outside a module

喜夏-厌秋 提交于 2020-05-30 08:13:08
问题 I was watching this video in order to learn how to add some simple tests to my Express routes but I am getting all kind of errors while executing a test. The error is: import * as chai from 'chai'; ^^^^^^ SyntaxError: Cannot use import statement outside a module I have read some similar Stack Overflow questions and GitHub issues but I didn't find a solution for my own application. Finally I found Mocha documentation on GitHub regarding ES modules but it didn't work: I created the app using

Paradoxical issue with mocha done() and async await

别来无恙 提交于 2020-05-26 11:45:05
问题 I have the following test case: it("should pass the test", async function (done) { await asyncFunction(); true.should.eq(true); done(); }); Running it asserts: Error: Resolution method is overspecified. Specify a callback or return a Promise; not both. And if I remove the done(); statement, it asserts: Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves. How to solve this paradox? 回答1: You need to remove the done

How to call assert for several time until it's true?

别来无恙 提交于 2020-05-19 04:51:03
问题 Say that I got some basic assert as below: expect(myObject.getValue()).to.equal(5); And the returned value from myObject.getValue() should be 5 after several code running over some other place, so I just need to let this value to be updated. My question is, what is the code for creating this kind of test? 回答1: Mocha has a facility for retrying tests. You can just use this.retries(number_of_tries) in your test. Like this: it("something", function () { this.retries(10); expect(myObject.getValue

How to call assert for several time until it's true?

我是研究僧i 提交于 2020-05-19 04:50:53
问题 Say that I got some basic assert as below: expect(myObject.getValue()).to.equal(5); And the returned value from myObject.getValue() should be 5 after several code running over some other place, so I just need to let this value to be updated. My question is, what is the code for creating this kind of test? 回答1: Mocha has a facility for retrying tests. You can just use this.retries(number_of_tries) in your test. Like this: it("something", function () { this.retries(10); expect(myObject.getValue

unit test mocha Visual Studio Code describe is not defined

主宰稳场 提交于 2020-05-11 03:55:40
问题 If i run in the console the test runs fine mocha --require ts-node/register tests/**/*.spec.ts Note: I installed mocha and mocha -g I want to run unit test from Visual Studio Code launcgh.js file "version": "0.2.0", "configurations": [ { "type": "node", "request": "launch", "name": "Mocha Tests", "program": "${workspaceFolder}/node_modules/mocha/bin/_mocha", "args": [ "--require", "ts-node/register", "-u", "tdd", "--timeout", "999999", "--colors", "${workspaceFolder}/tests/**/*.spec.ts" ],