首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >与Nuxt和Jest的集成测试

与Nuxt和Jest的集成测试
EN

Stack Overflow用户
提问于 2018-09-25 07:55:51
回答 1查看 2.2K关注 0票数 1

我希望使用Nuxt的renderAndGetWindow呈现一个页面,以测试我的API返回的值。

我是这样做的:

代码语言:javascript
复制
// 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)

自从我从艾娃转到杰斯之后我就得到了这个。

我在处理渲染错误吗?

EN

回答 1

Stack Overflow用户

发布于 2018-09-25 19:14:53

未参考

jsdom

unrefNode提供的一种特殊功能。它不是在浏览器或jsdom中实现的,而是Jest实现的。

看起来,测试Nuxt应用程序需要一个Node环境来启动服务器,并且需要一个jsdom环境来测试结果的UI。

这可以通过将测试环境设置为“节点”并在测试设置期间使用window初始化jsdom来实现。

超时

Jest will ".这适用于测试函数和设置函数(如beforeAll )。

您的beforeAll函数有一个从未调用过的参数doneJest将等待done被调用或使用jest.setTimeout配置的超时过期(默认为5秒)。

您正在使用async函数,并且在函数的异步部分上使用await,因此不需要done。将beforeAll函数更改为不接受任何参数,这将防止Jest等待调用done

在我的测试中,启动Nuxt服务器需要很长时间,因此您可以将一个timeout值作为附加参数传递给beforeAll,以增加该函数的超时时间。

下面是对这些更改的更新测试:

代码语言:javascript
复制
/**
 * @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', () => {
  });
});
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52493255

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档