我的测试组中有两个测试。一个测试使用它,另一个使用测试。它们的工作原理似乎非常相似。它们之间的区别是什么?

describe('updateAll', () => {
  it('no force', () => {
    return updateAll(TableName, ["fileName"], {compandId: "test"})
        .then(updatedItems => {
          let undefinedCount = 0;
          for (let item of updatedItems) {
            undefinedCount += item === undefined ? 1 : 0;
          }
          // console.log("result", result);
          expect(undefinedCount).toBe(updatedItems.length);
        })
  });

  test('force update', () => {
    return updateAll(TableName, ["fileName"], {compandId: "test"}, true)
        .then(updatedItems => {
          let undefinedCount = 0;
          for (let item of updatedItems) {
            undefinedCount += item === undefined ? 1 : 0;
          }
          // console.log("result", result);
          expect(undefinedCount).toBe(0);
        })
  });
});

更新- 2022年11月:

根据Jest的官方API,测试和它似乎是可以互换的。正如这里所描述的@gwildu,出于可读性考虑,您应该选择其中一种。


当前回答

`const request = require('supertest');`
`const app =  require('../app')`
`const {it, describe} = require('@jest/globals');`
`const { sequelize, Category, User, News , Customer, Bookmark} = require('../models');`
`const {hash} = require('../helpers/bcrypt');`
`const news = require('../models/news');`
`const {queryInterface} = sequelize`


`beforeAll(async()=>{

    let userData = require("../data/user.json")
    userData.forEach(el => {
        el.password = hash(el.password)
        el.createdAt = new Date()
        el.updatedAt = new Date()
    });`

`afterAll(async ()=>{
    await Bookmark.destroy({
        truncate: true,
        cascade: true,
        restartIdentity: true
    })`

`describe('GET /customers/news', () => {
    it("should response with status 200 1", async () => {
        let response = await request(app)
        .get('/customers/news')
        // .set({access_token})
        // console.log(response.body, "<<<<NEWS NIhH");
        expect(response.status).toBe(200)
        expect(response.body).toBeInstanceOf(Array)
    })`

`it("should response with status 200 2", async()=>{
    let response = await request(app)
    .get('/customers/news?filter=1,2')
    expect(response.status).toBe(200)
    expect(response.body).toBeInstanceOf(Array)
})`

其他回答

Jest还没有提到为什么他们有两个版本的完全相同的功能。

我猜,这只是惯例。Test用于单元测试,也用于集成测试。

它们做同样的事情,但它们的名字不同,因此它们与测试名称的交互作用也不同。

test

你写什么:

describe('yourModule', () => {
  test('if it does this thing', () => {});
  test('if it does the other thing', () => {});
});

如果某事失败了你会得到什么:

yourModule > if it does this thing

it

你写什么:

describe('yourModule', () => {
  it('should do this thing', () => {});
  it('should do the other thing', () => {});
});

如果某事失败了你会得到什么:

yourModule > should do this thing

所以这是关于可读性而不是功能性。

在我看来,阅读不是自己写的失败测试的结果是有意义的。它有助于更快地理解测试的内容。

一些开发人员还将Should do this thing缩短为Does this thing,这更短一些,而且在语义上也符合it符号。

正如笑话文档所说,它们是一样的: 它的别名

测试(名称,fn,超时) 同样在别名下:it(name, fn, timeout)

描述只是为了让你的测试被分成几组 描述

描述(名字,fn)

Describe (name, fn)创建一个将几个相关测试分组在一起的块。例如,如果你有一个myBeverage对象,它应该是美味的,但不是酸的,你可以用:

const myBeverage = {
  delicious: true,
  sour: false,
};

describe('my beverage', () => {
  test('is delicious', () => {
    expect(myBeverage.delicious).toBeTruthy();
  });

  test('is not sour', () => {
    expect(myBeverage.sour).toBeFalsy();
  });
});

这不是必需的—您可以直接在顶层编写测试块。但是如果你喜欢把你的测试组织成小组,这就很方便了。

Jest文档声明它是test的别名。所以从功能的角度来看它们是完全一样的。它们的存在都是为了让你在考试中写出一个可读的英语句子。

`const request = require('supertest');`
`const app =  require('../app')`
`const {it, describe} = require('@jest/globals');`
`const { sequelize, Category, User, News , Customer, Bookmark} = require('../models');`
`const {hash} = require('../helpers/bcrypt');`
`const news = require('../models/news');`
`const {queryInterface} = sequelize`


`beforeAll(async()=>{

    let userData = require("../data/user.json")
    userData.forEach(el => {
        el.password = hash(el.password)
        el.createdAt = new Date()
        el.updatedAt = new Date()
    });`

`afterAll(async ()=>{
    await Bookmark.destroy({
        truncate: true,
        cascade: true,
        restartIdentity: true
    })`

`describe('GET /customers/news', () => {
    it("should response with status 200 1", async () => {
        let response = await request(app)
        .get('/customers/news')
        // .set({access_token})
        // console.log(response.body, "<<<<NEWS NIhH");
        expect(response.status).toBe(200)
        expect(response.body).toBeInstanceOf(Array)
    })`

`it("should response with status 200 2", async()=>{
    let response = await request(app)
    .get('/customers/news?filter=1,2')
    expect(response.status).toBe(200)
    expect(response.body).toBeInstanceOf(Array)
})`