-
Notifications
You must be signed in to change notification settings - Fork 2
[Beginner] Test a Function
loripam edited this page Dec 20, 2021
·
5 revisions
- On your directory in VS Code, create
New File
undertest
and make sure the file name ends with.test.ts
so it matches the formula onpackage.json
below:
test/**/<name>.test.ts
- Use the snippet
learn-new-normal-test-file
. This imports the test runnerava
and the test modules if you have them. Create the test by filling out the fields on thelearn-new-normal-test-file
snippet.
test('description', t => {
})
- Define your test by creating a function. Use the
function
snippet.
function name(params:type) {
}
- Run the test by typing
npm test
on the terminal.
const result = func(a)
t.is(result, x);
✔️ If it passes then you're good to go.
- Move the
function
to a new file by refactoring it. Highlight thefunction
➡️ right-click ➡️Refactor
➡️Move to a new file
. Move that new file undersrc
.
✔️ Notice that the test will still pass even if you move it.
- Any time you have an npm script, you can put in
npm test -- -w
to launch a watch flag. This will allowava
to keep on running and continuously test whenever there are new changes.
❗ Need help? Reach out to us on NoStack's Discord Server/Community. We'll make sure to respond to you as soon as possible!