supertest

How to mock a middleware in supertest?

﹥>﹥吖頭↗ 提交于 2020-07-18 07:01:49
问题 I want to test if the middleware in app.js is called. Although I mock the module work.js , it still runs the original code. app.js const work = require('./work') const express = require('require') const app = express() .use(work) .use(...) .get(...) module.exports = app work.js function work(req, res, next){...} module.exports = work app-test.js const supertest = require('supertest') const app = require('../app') test('test middleware in app.js', async(done) => { jest.mock('../work', () =>

Order of functions being executed in Express messed with tests

泪湿孤枕 提交于 2020-04-18 05:47:47
问题 I was trying to integrate Jest and Supertest to build integration tests on some middleware. I generated my middleware functions dynamically as they varied route to route, and the look like this: export function middleware1(param: paramType) { return async (req: Request, res: Response, next: NextFunction) => { ... }; } In my Jest tests, at the top of the file, I mock middleware1 as so: jest.mock('../middleware_path', () => ({ middleware1: jest.fn( _ => { return (req, res, next) => { return new

Order of functions being executed in Express messed with tests

自作多情 提交于 2020-04-18 03:48:36
问题 I was trying to integrate Jest and Supertest to build integration tests on some middleware. I generated my middleware functions dynamically as they varied route to route, and the look like this: export function middleware1(param: paramType) { return async (req: Request, res: Response, next: NextFunction) => { ... }; } In my Jest tests, at the top of the file, I mock middleware1 as so: jest.mock('../middleware_path', () => ({ middleware1: jest.fn( _ => { return (req, res, next) => { return new

Order of functions being executed in Express messed with tests

时光总嘲笑我的痴心妄想 提交于 2020-04-18 03:47:06
问题 I was trying to integrate Jest and Supertest to build integration tests on some middleware. I generated my middleware functions dynamically as they varied route to route, and the look like this: export function middleware1(param: paramType) { return async (req: Request, res: Response, next: NextFunction) => { ... }; } In my Jest tests, at the top of the file, I mock middleware1 as so: jest.mock('../middleware_path', () => ({ middleware1: jest.fn( _ => { return (req, res, next) => { return new

middleware must be a function for koa passport testing using jest typescript

大兔子大兔子 提交于 2020-04-07 08:11:53
问题 Running into a few issues when trying to mock an oauth2 flow using passport and koa... The error when running test: TypeError: middleware must be a function! 26 | app.use(bodyParser()); > 27 | app.use(authRouting as any); | ^ auth.ts: import { KoaPassport } from 'koa-passport'; import * as Router from 'koa-router'; import { signAccessToken } from '../utils/jwt'; export function createAuthRoutes(passport: InstanceType<typeof KoaPassport>) { const router = new Router(); router.get('/api/login',

supertest expect(function(res){}) , Error: expected [Function] response body ??

孤者浪人 提交于 2020-01-25 12:02:08
问题 I'm using supertest and mocha testing my express rest api. there's this test case I want to check the returned response body with a method of supertest:expect(function(res){ } ). But I'm facing an error that I can't figure out why: Error: expected [Function] response body, got '{"name":"Aaron Shen","_id":" 530ed1ce92788ed031022d8c","__v":0,"active":true}' Does anybody know how to fix? below is my testing code: it('should return correct player',function(done){ var url = '/api/players/' + pid;

supertest expect(function(res){}) , Error: expected [Function] response body ??

自作多情 提交于 2020-01-25 12:01:05
问题 I'm using supertest and mocha testing my express rest api. there's this test case I want to check the returned response body with a method of supertest:expect(function(res){ } ). But I'm facing an error that I can't figure out why: Error: expected [Function] response body, got '{"name":"Aaron Shen","_id":" 530ed1ce92788ed031022d8c","__v":0,"active":true}' Does anybody know how to fix? below is my testing code: it('should return correct player',function(done){ var url = '/api/players/' + pid;

Node.js / Express / Mocha / Supertest Rest API - Empty Request Body

房东的猫 提交于 2020-01-13 07:49:24
问题 I've looked everywhere I can to find a solution to this. The only thing I've found is an unanswered post. I apologize if I've overlooked something. The problem is that when I try to get the POST values in the /createQuestion API, the body is empty/undefined. I get errors like this Cannot read proprety 'question' of undefined coming from the API. The Express API: app.post("/createQuestion", function(req, res) { var questionType = req.body.question.type; var questionText = req.body.question

Run jest test suites in groups?

偶尔善良 提交于 2020-01-11 09:41:25
问题 I am writing extensive tests for a new API via jest and supertest. Prior to running the tests, I am setting up a test database and populating it with users: Test command jest --forceExit --config src/utils/testing/jest.config.js jest.config.js module.exports = { rootDir: process.cwd(), // Sets up testing database with users globalSetup: './src/utils/testing/jest.setup.js', // Ensures connection to database for all test suites setupTestFrameworkScriptFile: './src/utils/testing/jest.db.js', }

Run jest test suites in groups?

隐身守侯 提交于 2020-01-11 09:41:10
问题 I am writing extensive tests for a new API via jest and supertest. Prior to running the tests, I am setting up a test database and populating it with users: Test command jest --forceExit --config src/utils/testing/jest.config.js jest.config.js module.exports = { rootDir: process.cwd(), // Sets up testing database with users globalSetup: './src/utils/testing/jest.setup.js', // Ensures connection to database for all test suites setupTestFrameworkScriptFile: './src/utils/testing/jest.db.js', }