Skip to content

[Beginner] Test a Function

loripam edited this page Dec 20, 2021 · 5 revisions

Steps

  1. On your directory in VS Code, create New File under test and make sure the file name ends with .test.ts so it matches the formula on package.json below:
test/**/<name>.test.ts

step 1


  1. Use the snippet learn-new-normal-test-file. This imports the test runner ava and the test modules if you have them. Create the test by filling out the fields on the learn-new-normal-test-file snippet.
test('description', t => {
})

step 2 a

step 2 b


  1. Define your test by creating a function. Use the function snippet.
function name(params:type) {
    
}

step 3


  1. 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.


  1. Move the function to a new file by refactoring it. Highlight the function ➡️ right-click ➡️ Refactor ➡️ Move to a new file. Move that new file under src.

✔️ Notice that the test will still pass even if you move it.

Notes

  • Any time you have an npm script, you can put in npm test -- -w to launch a watch flag. This will allow ava to keep on running and continuously test whenever there are new changes.
Clone this wiki locally