import describe/it

김동현·2024년 2월 17일

테스트코드

목록 보기
1/7
post-thumbnail

describe/it

describe/it이 .test.{js | ts} 파일 내에서 Jest 라이브러리에서 제공하는 것인 줄 알았다.
하지만, 알고 보니 Nodejs의 것이었다.

describe/it syntax

Running tests can also be done using describe to declare a suite and it to declare a test. A suite is used to organize and group related tests together. it is a shorthand for test().

// index.js
import { strict as assert } from 'assert';
import { describe, it, test, mock, before, after } from "node:test";

describe('A thing', () => {
    it('should work', () => {
        assert.strictEqual(1, 1);
    });

    it('should be ok', () => {
        assert.strictEqual(2, 2);
    });

    describe('a nested thing', () => {
        it('should work', () => {
            assert.strictEqual(3, 3);
        });
    });
});
// with jest 
// index.test.js
describe('1 + 2', () => {
    test('adds 1 + 2 to equal 3', () => {
        expect(1 + 2).toBe(3);
    });
});

🤔 import 하지 않았는데 어떻게 쓰고 있었지?

In your test files, Jest puts each of these methods and objects into the global environment. You don't have to require or import anything to use them. However, if you prefer explicit imports, you can do import {describe, expect, test} from '@jest/globals'.

Jest Global

Jest Docs의 Global 부분의 최상단 내용을 보면 테스트 파일에서 Jest는 메서드들을 전역 환경에 넣어놓는다고 한다.
즉, 테스트 코드를 사용하기 위해 describeit 같은 것들을 import 하지 않아도 되는 것이다!

Jest-Global을 들어가면 확인할 수 있다.

✅ 테스트 파일 정규 표현식은 jest-validaate의 jestConfig.ts에서 찾아볼 수 있다.

CHANGE.log에 보면 test.js spec.js 파일이 포함된 걸 볼 수 있다.
이전에는 __test__ 만 되었을까?? 🤔🤔

profile
달려보자

0개의 댓글