Tag: jestjs

testing后closures服务器和数据库连接

我有与mongo连接的koa服务器,并使用supertest模拟服务器和发送请求,作为testing框架。 const app = new Koa() … export default app.listen(PORT, (err) => { if (err) console.log(err) if (!IS_TEST) { console.log(`Server running on port: ${PORT}`) } }) 在成功完成testing或者服务器连接失败之后,koa服务器连接testing结束多久? testing例子: import supertest from 'supertest' import mongoose from 'mongoose' import server from '../../../app/server' import User from '../../../app/models/user' const r = supertest.agent(server.listen()) afterEach(async (done) => { await mongoose.connection.db.dropDatabase() done() […]

用Postgrestestingasynchronous错误

我一直在用Postgres数据库编写Sequelize的unit testing。 为了testing,我正在使用Jest。 现在,我可以得到这个代码工作: test('email field only takes valid emails', () => { expect.assertions(2); let user = Users.build({ email: 'notAProperEmail', password: 'abc123' }); return user.validate().catch(err => { expect(err).toHaveProperty('errors'); expect(err.errors[0].message).toEqual('Validation isEmail on email failed'); }); }) 但由于某种原因,这会抛出错误“SequelizeDatabaseError:关系”用户“不存在”。 此外,预期的方法也永远不会被击中,因为我得到了预期的断言错误:“期望的一个断言被调用,但接收到零断言调用”。 describe('Password is encrypted', () => { beforeAll(() => { Users.create({ email: 'test1@gmail.com', password: 'testPassword123' }) }); test('password is […]

我怎样才能使用JEST间谍来testing一个NodeJs模块/应用程序

我不明白如何侦测一个模块内部的方法/函数是否被触发。 这是我的例子: db.js module.exports.saveUser = (user) => { console.log('Saving the user', user); }; app.js let db = require('./db'); module.exports.handleSignup = (email, password) => { db.saveUser({ email, password }); } app.test.js const db = require('./db'); jest.genMockFromModule('./app'); const app = require('./app'); describe('App: ', () => { it('should call "db.saveUser" with a user object', () => { let […]

笑npm脚本的正则expression式

我想执行2个不同的testing环境, 一个用于integration tests ,另一个用于unit tests 。 文件夹结构是这样的: __tests__/ut/p/a/t/h/file.test.ts __tests__/it/p/a/t/h/file.test.ts 我拿出了这些正则expression式: (__tests__/ut/.*\.test)\.(tsx?|ts?)$ (__tests__/it/.*\.test)\.(tsx?|ts?)$ 我的脚本如下所示: "test:ut": "jest \"(__tests__/ut/.*\\.test)\\.(tsx?|ts?)$\"", "test:it": "jest \"(__tests__/it/.*\\.test)\\.(tsx?|ts?)$\"", 但是当我运行yarn test:ut我得到: yarn run v1.3.2 $ jest "(__tests__/ut/.*\.test)\.(tsx?|ts?)$" No tests found In /home/dev/sample 20 files checked. testMatch: **/__tests__/**/*.js?(x),**/?(*.)(spec|test).js?(x) – 0 matches testPathIgnorePatterns: /node_modules/ – 20 matches Pattern: (__tests__/ut/.*\.test)\.(tsx?|ts?)$ – 0 matches Done in 0.49s. 虽然,如果我在jsonconfiguration文件中使用相同的模式,它工作。 我想我是如何执行这个jest […]

承诺拒绝不在回拨里面开玩笑

我试图提高现有项目的testing覆盖率,尝试探索笑话的嘲笑(说实话,我对NodeJS来说是一个非常新的东西,来自Java背景),并且认为我对它有基本的了解。 然而,我遇到了一个问题,我仍然无法弄清楚为什么笑话是这样的。 我正在编写testing的示例实现(handler.js): 'use strict'; const profileStore = require('./store/profile-store'); module.exports.invoke = (event, context, callback) => { let id = context.id; // profileStore.read returns a Promise profileStore.read(id) .then((profile) => onSuccess(profile)) .catch((err) => onErrorProfile(err)); function onSuccess(profile) { callback(null, 'Found Profile: ' + profile.name); }; function onErrorProfile(err) { callback(null, 'Error finding Profile'); }; }; testing文件(handler.test.js) const handlerImp = […]

如何正确地testing需要newrelic的模块

我正在从一些嘲讽的工作中迁移出一堆unit testing。 当我要求一个需要新代理的模块时: require('newrelic') ,我得到下面的错误: – TypeError: Cannot convert undefined or null to object at Object.<anonymous> (node_modules/newrelic/lib/config.js:165:33) at Runtime._execModule (node_modules/jest-cli/src/Runtime/Runtime.js:261:17) at Object.<anonymous> (node_modules/newrelic/lib/logger.js:18:14) at Object.<anonymous> (node_modules/newrelic/index.js:3:14) 什么是最好的方式来处理模块,像newrelic这个笑话难以嘲笑? 当其他人在他们的堆栈中同时开玩笑和新玩意时,他们做了什么?

Jest JStesting覆盖率数据不会从Codeship发布到代码气候

我有一个基于Node.js的项目,我正在使用Jest进行.jsunit testing。 我将collectCoverageconfiguration设置设置为true并可以在本地开发运行中查看testing覆盖表,以及何时在Codeship中运行构build。 但是,我似乎无法得到testing覆盖率数据在Code Climate中显示的结果。 我将CodeClimate的设置说明中给出的回购令牌保存到Codeship环境variables中,如下所示: CODECLIMATE_REPO_TOKEN=xxxxxxxxxx codeclimate-test-reporter < lcov.info 鉴于在本地,JEST collectCoverage将lcov.info数据文件保存在我的项目根目录下的一个名为coverage的目录中,我也尝试过使用该path的环境variables的变体,例如: CODECLIMATE_REPO_TOKEN=xxxxxxxxxx codeclimate-test-reporter < coverage/lcov.info 对于codeclimate-test-reporter节点模块( npm install -g codeclimate-test-reporter )的npm install -g codeclimate-test-reporter ,我不清楚它应该到哪里去。 我试过把它: 在通用npm install命令之前的Codeshiptesting设置设置命令部分, 在Codeshiptestingpipe道configuration命令部分中, 我也试过在我的package.json中将其作为本地开发依赖项进行安装。 上述尝试结合起来,导致在Code Climate方面看到有关testing覆盖率的任何内容。 作为我所做的最后一点,我没有提交我的本地coverage目录,所以它不包含在我的Codeship版本中。 任何人都可以帮我弄清楚我做错了什么?

SyntaxError:意外的标记{在jesttesting期间加载节点模块时

我正在运行一个依赖于“ant”节点模块的笑话testing 这似乎是不能正确parsing的atnd模块内的CSS文件。 我该如何解决这个问题? 当我运行它失败,如下所示: C:\path\to\project\node_modules\antd\lib\style\index.css:10 html { ^ SyntaxError: Unexpected token { at transformAndBuildScript (node_modules\jest-cli\node_modules\jest-runtime\build\transform.js:316:10) at Object.<anonymous> (node_modules\antd\lib\switch\style\css.js:3:1) 我的babelrc如下: { "presets": [ "react", "es2015-loose", "stage-0" ], "plugins": [ "babel-root-slash-import", "transform-decorators-legacy", "react-hot-loader/babel", "transform-runtime", ["import", [{"libraryName": "antd", "style" : true}]] ], "compact": true, "ignore": [ "/node_modules/(?!react-number-input)" ] } jest.json { "setupTestFrameworkScriptFile": "<rootDir>/jasmine-setup-env.js", "bail": false, "collectCoverage": true, "collectCoverageFrom": […]

用笑话嘲笑mongoose模型

我试图用jest来嘲笑mongoose模型,但越来越Cannot create property 'constructor' on number '1'错误的Cannot create property 'constructor' on number '1' 。 我能够通过创build具有以下2个文件的项目来重现该问题。 有没有办法用jest模仿mongoose模型? ./model.js const mongoose = require('mongoose') const Schema = mongoose.Schema const schema = new Schema({ name: String }) module.exports = mongoose.model('Test', schema) ./model.test.js jest.mock('./model') const Test = require('./model') // Test.findOne.mockImplementation = () => { // … // } 错误: FAIL […]

编码在jest.js中不被识别

我有一个问题,使用node-mysql2testing项目,反应,续集和笑话。 这个问题只发生在testing过程中。 Encoding not recognized: 'cesu8' (searched as: 'cesu8') at Object.getCodec (project/node_modules/mysql2/node_modules/iconv-lite/lib/index.js:106:23) at Object.getDecoder (project/node_modules/mysql2/node_modules/iconv-lite/lib/index.js:122:23) at Object.<anonymous>.exports.decode (project/node_modules/mysql2/lib/parsers/string.js:9:23) at Packet.Object.<anonymous>.Packet.readNullTerminatedString (project/node_modules/mysql2/lib/packets/packet.js:373:23) at Function.Object.<anonymous>.Handshake.fromPacket (project/node_modules/mysql2/lib/packets/handshake.js:18:31) at ClientHandshake.Object.<anonymous>.ClientHandshake.handshakeInit (project/node_modules/mysql2/lib/commands/client_handshake.js:98:38) at ClientHandshake.Object.<anonymous>.Command.execute (project/node_modules/mysql2/lib/commands/command.js:40:20) at Connection.Object.<anonymous>.Connection.handlePacket (project/node_modules/mysql2/lib/connection.js:515:28) at PacketParser.onPacket (project/node_modules/mysql2/lib/connection.js:94:16) at PacketParser.executeStart (project/node_modules/mysql2/lib/packet_parser.js:77:14) at Socket.<anonymous> (project/node_modules/mysql2/lib/connection.js:102:29)