supertest和vue的开玩笑配置

发布于 2025-02-07 07:00:53 字数 1894 浏览 7 评论 0原文

我有一个使用VUE和Express的应用程序,并且我都为每个应用程序编写了测试,我可以自己进行VUE测试,也可以单独进行Express Test。

下面是嘲笑配置文件,如果我删除预设,则可以很好地运行VUE,对于我的supertest/Express测试将正常工作。

module.exports = {
// TODO: This line is needed for vue tests but breaks js tests
preset: "@vue/cli-plugin-unit-jest",


coverageDirectory: "test-reports/",
    coveragePathIgnorePatterns: [
      "/node_modules/"
    ],
  reporters: [
    "default",
    [
      "jest-junit",
      {
        outputName: "vue_junit.xml",
        outputDirectory: "test-reports/",
      },
    ],

  ],
};

在这里参考,这两个测试是否有助于

// Libraries
import Vuetify from "vuetify";
import Vue from "vue";

// Components
import UserBar from "@/components/userBar.vue"

// Utilities
import { createLocalVue, shallowMount } from "@vue/test-utils";

// Setup Up
const localVue = createLocalVue();
const vuetify = new Vuetify();
Vue.use(Vuetify);

describe("userBar.vue", () => {
  let wrapper;

  beforeEach(() => {
    wrapper = shallowMount(UserBar, {
      localVue,
      vuetify,
    });
  });
  afterEach(() => {
    wrapper.destroy();
  });

  it("wrapper created properly", () => {
    expect(typeof wrapper).toBe("object");
  });
});

表达测试

var request = require('supertest');
import * as local from '../../server';

var express = require('express');
var app = express();
describe('loading express', function () {
  var server;
  beforeEach(function () {
  delete require.cache[require.resolve('../../server/index')];
  server = local.default(app);
});
  afterEach(function (done) {
    server.close(done);
  });
  it('responds to /client', function testSlashHealth(done) {
  request(server)
    .get('/client')
    .expect(200, done);
  });
  it('404 everything else', function testPath(done) {
    request(server)
      .get('/foo/bar')
      .expect(404, done);
  });
});

I have an application that uses vue and express and I have tests written for each, I can run either a vue test on it's own or the express test on it's own.

Below is the jest config file this will run fine for Vue, if I remove the preset it will work fine for my supertest/express test .

module.exports = {
// TODO: This line is needed for vue tests but breaks js tests
preset: "@vue/cli-plugin-unit-jest",


coverageDirectory: "test-reports/",
    coveragePathIgnorePatterns: [
      "/node_modules/"
    ],
  reporters: [
    "default",
    [
      "jest-junit",
      {
        outputName: "vue_junit.xml",
        outputDirectory: "test-reports/",
      },
    ],

  ],
};

For reference here is both the tests if it helps

// Libraries
import Vuetify from "vuetify";
import Vue from "vue";

// Components
import UserBar from "@/components/userBar.vue"

// Utilities
import { createLocalVue, shallowMount } from "@vue/test-utils";

// Setup Up
const localVue = createLocalVue();
const vuetify = new Vuetify();
Vue.use(Vuetify);

describe("userBar.vue", () => {
  let wrapper;

  beforeEach(() => {
    wrapper = shallowMount(UserBar, {
      localVue,
      vuetify,
    });
  });
  afterEach(() => {
    wrapper.destroy();
  });

  it("wrapper created properly", () => {
    expect(typeof wrapper).toBe("object");
  });
});

express test

var request = require('supertest');
import * as local from '../../server';

var express = require('express');
var app = express();
describe('loading express', function () {
  var server;
  beforeEach(function () {
  delete require.cache[require.resolve('../../server/index')];
  server = local.default(app);
});
  afterEach(function (done) {
    server.close(done);
  });
  it('responds to /client', function testSlashHealth(done) {
  request(server)
    .get('/client')
    .expect(200, done);
  });
  it('404 everything else', function testPath(done) {
    request(server)
      .get('/foo/bar')
      .expect(404, done);
  });
});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

微暖i 2025-02-14 07:00:53

测试时通过指定2个配置文件来解决此问题

jest -c jest.config.express.js

jest -c jest.config.vue.js

Got around this by specifying 2 config files when testing

jest -c jest.config.express.js

jest -c jest.config.vue.js

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文