In the previous lesson, when we run Vtest in a new project, we got no test files found error. So how could we create those test files? js and Vtest could recognize it as a test file and automatically run it. Run the test again, we will see a different error saying no test should find in the file.
Of course, it's because our test file is empty. So let's add some tests to it. First, we need to import some functions from the test. Here we just need test and expect for this demo.
The test function describes a chunk of functionalities you want to test. The first argument will be a string to describe what the test is about. And the second argument will be a function. That is what the real test lies.
Here we could start testing the summary of 1 plus 1. Then we can use expect function from vtest to do the assertion. It's pretty semantic. We can say expect 1 plus 1 to equals 2.
And then we are done. As you can see, there are quite some different assertions we could make with expect. That's just to echo for now and we'll dive into the others in the future lessons. When we save the file, we just wrote, and we will see our test is passing.
Great! One thing you might notice is that when we are writing the test, VDAS does not exit. Instead, it keeps running and reruns the test whenever we make changes. That's one of the great features VDAS provides.
It runs the watch mode by default, just like the VDAS dev server. This also means you can play with the test and get feedbacks in real time. Now, let's just make some modifications to the assertion we just made. We could change 2 equal 2 to equal 3, which obviously wrong and we can expect the test to fail.
Then we hit save, we will see VITES reruns the test immediately. This time, however, the test fails since 2 is not equal to 3 as we expected. Let's change it back to 2 and the test is passing again instantly. In the previous lesson, when we run Vtest in a new project, we got no test files found error.
So how could we create those test files? js and Vtest could recognize it as a test file and automatically run it. Run the test again, we will see a different error saying no test should find in the file. Of course, it's because our test file is empty.
So let's add some tests to it. First, we need to import some functions from the test. Here we just need test and expect for this demo. The test function describes a chunk of functionalities you want to test.
The first argument will be a string to describe what the test is about. And the second argument will be a function. That is what the real test lies. Here we could start testing the summary of 1 plus 1.
Then we can use expect function from vtest to do the assertion. It's pretty semantic. We can say expect 1 plus 1 to equals 2. And then we are done.
As you can see, there are quite some different assertions we could make with expect. That's just to echo for now and we'll dive into the others in the future lessons. When we save the file, we just wrote, and we will see our test is passing. Great!
One thing you might notice is that when we are writing the test, VDAS does not exit. Instead, it keeps running and reruns the test whenever we make changes. That's one of the great features VDAS provides. It runs the watch mode by default, just like the VDAS dev server.
This also means you can play with the test and get feedbacks in real time. Now, let's just make some modifications to the assertion we just made. We could change 2 equal 2 to equal 3, which obviously wrong and we can expect the test to fail. Then we hit save, we will see VITES reruns the test immediately.
This time, however, the test fails since 2 is not equal to 3 as we expected. Let's change it back to 2 and the test is passing again instantly.