Eai
Eai10mo ago

In-Source Testing with deno test?

Can I do something like vitest's In-Source Testing with deno test? That is, I don't want to export private functions or objects just for testing.
Vitest
Next generation testing framework powered by Vite
2 Replies
Deno AI Helper
Deno AI Helper10mo ago
I wasn't able to find the answer to your question in the official Deno documentation. Deno's built-in testing capabilities are typically used by writing separate test files or by using the Deno.test function within your modules. However, based on your message, if you're looking to test private functions or objects without exporting them, you might consider the following approaches, although please note that this advice is not based on the Deno documentation, and you should double-check the accuracy of this advice: 1. Testing private functions within the same module: You can write your tests in the same module where the private functions are defined using Deno.test. This way, you can access the private functions directly without exporting them. 2. Using top-level await: If you're writing your tests in the same file, you can use top-level await to run your tests after your module's code. Here's a simple example of how you might structure your tests within the same module:
// my_module.ts

function privateFunction() {
// private logic
return true;
}

// ... rest of your module code ...

// At the bottom of your module, you can include your tests
if (import.meta.main) {
Deno.test('privateFunction test', () => {
// Your test logic here
const result = privateFunction();
// Assertions for result
});
}
// my_module.ts

function privateFunction() {
// private logic
return true;
}

// ... rest of your module code ...

// At the bottom of your module, you can include your tests
if (import.meta.main) {
Deno.test('privateFunction test', () => {
// Your test logic here
const result = privateFunction();
// Assertions for result
});
}
Remember to run your tests with deno test. Please keep in mind that this pattern is not documented in the official Deno documentation, and it's generally a good practice to keep your tests separate from your module code. This approach is just a workaround and may not be the best practice for maintainability and separation of concerns. It's always a good idea to check the latest Deno documentation or seek advice from the
Deno AI Helper
Deno AI Helper10mo ago
Deno community for the most up-to-date and idiomatic testing practices.

Did you find this page helpful?