我希望使用Nuxt的renderAndGetWindow呈现一个页面,以测试我的API返回的值。
我是这样做的:
// Nuxt instance
let nuxt = null;
// Our page to test
let homePage = null;
beforeAll(async (done) => {
// Configuration
const rootDir = resolve(__dirname, '../..');
let config = {};
config = require(resolve(rootDir, 'nuxt.config.js'));
config.rootDir = rootDir; // project folder
config.env.isDev = false; // dev build
config.mode = 'universal'; // Isomorphic application
nuxt = new Nuxt(config);
await new Builder(nuxt).build();
nuxt.listen(3001, 'localhost');
homePage = await nuxt.renderAndGetWindow('http://localhost:3001/');
});这给了我两个截然不同的错误:
node_modules/jest-jasmine2/build/jasmine/Env.js:157 console.error未处理错误 node_modules/jest-jasmine2/build/jasmine/Env.js:158 TypeError: setInterval(.).unref不是函数
和
超时-异步回调没有在jest.setTimeout指定的5000 by超时内调用。 在地图(node_modules/jest-jasmine2/build/queue_runner.js:41:52)
自从我从艾娃转到杰斯之后我就得到了这个。
我在处理渲染错误吗?
发布于 2018-09-25 19:14:53
未参考
unref是Node提供的一种特殊功能。它不是在浏览器或jsdom中实现的,而是Jest实现的。
看起来,测试Nuxt应用程序需要一个Node环境来启动服务器,并且需要一个jsdom环境来测试结果的UI。
这可以通过将测试环境设置为“节点”并在测试设置期间使用window初始化jsdom来实现。
超时
Jest will ".这适用于测试函数和设置函数(如beforeAll )。
您的beforeAll函数有一个从未调用过的参数done。Jest将等待done被调用或使用jest.setTimeout配置的超时过期(默认为5秒)。
您正在使用async函数,并且在函数的异步部分上使用await,因此不需要done。将beforeAll函数更改为不接受任何参数,这将防止Jest等待调用done。
在我的测试中,启动Nuxt服务器需要很长时间,因此您可以将一个timeout值作为附加参数传递给beforeAll,以增加该函数的超时时间。
下面是对这些更改的更新测试:
/**
* @jest-environment node
*/
// TODO: Set the environment to "node" in the Jest config and remove this docblock
// TODO: Move this to a setup file
const { JSDOM } = require('jsdom');
const { window } = new JSDOM(); // initialize window using jsdom
const resolve = require('path').resolve;
const { Nuxt, Builder } = require('nuxt');
// Nuxt instance
let nuxt = null;
// Our page to test
let homePage = null;
beforeAll(async () => {
// Configuration
const rootDir = resolve(__dirname, '../..');
let config = {};
config = require(resolve(rootDir, 'nuxt.config.js'));
config.rootDir = rootDir; // project folder
config.env.isDev = false; // dev build
config.mode = 'universal'; // Isomorphic application
nuxt = new Nuxt(config);
await new Builder(nuxt).build();
nuxt.listen(3001, 'localhost');
homePage = await nuxt.renderAndGetWindow('http://localhost:3001/');
}, 20000); // Give the beforeAll function a large timeout
afterAll(() => {
nuxt.close();
});
describe('homepage', () => {
it('should do something', () => {
});
});https://stackoverflow.com/questions/52493255
复制相似问题