Jest Flashcards

1
Q

How to install jest

A

npm i jest

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What should we name test files?

A

someName.test.js

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How to write a basic test?

A

const ct = require(“../src/math”);

test(“should calcuate the tip amount”, ()=>{

    const amount = ct(100,.1)
    expect(amount).toBe(10)
});
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How to write tests for async code?

A
test("Should work with async code", (done)=>{
    setTimeout(()=>{
        expect(1).toBe(2)
        done()
    },5000)
});

or

test("Should work with async code", async ()=>{
    await setTimeout(()=>{
        expect(1).toBe(2)
        done()
    },5000)
});
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How to configure jest in the package.json file?

A
{
  "name": "taskmanager",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "node src/index.js",
    "dev": "env-cmd -f ./config/dev.env nodemon src/index.js",
    "test": "env-cmd -f ./config/test.env jest --watch"
  },
  "jest": {
    "testEnvironment": "node"
  },
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

When testing a node application, why do we need to set the testEnvorionment to “node”

A

Because the defoult is “jsdom” which is a browser environment

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How to test api or http requests using jest and super test

A
  1. install supertest
  2. Copy the entire index.js file over into a new file that lives in the same folder, then delete the port and .listen function and export that file.
  3. import the file to index.js and delete everything except for the port and .listen function.
  4. in the test folder create the .test.js file
  5. import the app.js file, import the supertest as request
  6. Setup the test using request like follows
const request = require('supertest');
const server = require("../src/app");

test(‘Should create a new user’, async()=>{

    await request(server).post('/users').send({
        name:'Andre',
        email:'andre@mail.com',
        password:"1234abcd"
    }).expect(201)

});

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How to setup functions that need to run before each test?

A
beforeEach(async ()=>{
    await User.deleteMany();
    await new User(userOne).save()
});
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How to create mocks?

A

in the test directory create a __mocks__ folder.

Then create a folder inside of the __mocks__ folder with the name of the module or function to be mocked

inside that folder create the file and export a module containing the functions or calls to be mocked.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Which folder should be used to upload images during tests?

A

fixtures

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is the fixtures folder used for?

A

For images, files and other assests used during tests. Also for setups, like setting up databases before tests etc.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly