简体   繁体   中英

How to write unit test in different files in node js

I am writing a unit test for node application. All my unit tests are in a single file called test/index.ts. I want to separate each test into different files. Is there any way to achieve this?

Yes, generally you could just duplicate your file and when you run tests you will notice that each runs twice, unless your test command specifies a file, not a directory.

So you just need to create a new file in the test directory and populate it like the first.

If you tell me what command you use to run tests I can help you further.

Yes, you can. Just move some tests to a different file. Make sure you use describe() and it() in the new file. Then, let's say your tests are in tests folder, you can run mocha with mocha tests/**/*.ts (assuming ts extension). This will run all files with ts extension in tests folder.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM