Hi, I'm Tuan, a Full-stack Web Developer from Tokyo 😊. Follow my blog to not miss out on useful and interesting articles in the future.
😊Meet the NodeJS 18, bringing exciting new features to make your development experience even better! With the Standalone Test Runner, you can now test your code natively, making your life easier. Fetch Experimental Support introduces the much-loved fetch function to NodeJS, while the upgraded V8 Engine 10.1 offers enhanced performance and capabilities. Embrace NodeJS 18 and enjoy the friendly boost to your coding! 🌟
1. Standalone Test Runner
Testing is crucial for managing the risk of errors in production systems. Typically, developers use popular libraries like Jest, AVA, Supertest, and MochaJS to write tests in NodeJS. However, starting with NodeJS 18, there's a native way to implement tests. You can use the new API like this:
import test from 'node:test';
import assert from 'node:assert';
test('synchronous passing test', (t) => {
// This test passes because it does not throw an exception.
assert.strictEqual(1, 1);
});
test('synchronous failing test', (t) => {
// This test fails because it throws an exception.
assert.strictEqual(1, 2);
});
You can also use the context of the test()
function to create sub-tests:
import test from 'node:test';
import assert from 'node:assert';
test('top level test', async (t) => {
await t.test('subtest 1', (t) => {
assert.strictEqual(1, 1);
});
await t.test('subtest 2', (t) => {
assert.strictEqual(2, 2);
});
});
Keep in mind that you need to use the node:
prefix when importing the test module, or NodeJS will look for it in your node_modules
as an external package.
2. Fetch Experimental Support
The community is excited about this feature because it allows developers to use the fetch
function within NodeJS. In a browser context, you can use fetch
to make requests, like this:
function getProducts() {
fetch('https://api.site.com/api/v1/products')
.then(response => response.json())
.then(data => console.log(data));
}
You can also use async/await mode:
async function getProducts() {
const response = await fetch('https://api.site.com/api/v1/products');
const data = await response.json();
console.log(data);
}
These methods work in browsers but not in server-side contexts where you might want to make requests or connect to other services via fetch
. With NodeJS 18's experimental support, you can now use fetch
in NodeJS:
const res = await fetch('https://api.site.com/api/v1/products');
if (res.ok) {
const data = await res.json();
console.log(data);
}
This means you can make requests using fetch
just like in browsers.
3. V8 Engine Version 10.1
NodeJS 18 supports version 10.1 of the V8 engine, which comes with several new features:
- Added
findLast
andfindLastIndex
methods on arrays - More improvements in the
Intl.Locale
API - The
Intl.supportedValuesOf
function
The updated V8 JavaScript engine, version 10.1, originates from Chromium 101. With this update, Node.js 18 introduces the findLast()
and findLastIndex()
vector methods, the Intl.supportedValuesOf
function, enhancements to the Intl.Locale
API, and better performance for class fields and private class methods.
These improvements provide developers with more powerful tools and better performance for their NodeJS applications. As a result, you can expect faster processing, more efficient code execution, and overall improved performance in your projects using NodeJS 18.
Conclusion
In summary, NodeJS 18 brings essential features like the Standalone Test Runner, Fetch Experimental Support, and an updated V8 Engine to enhance productivity and performance. Start using these new features if you're running an existing application on NodeJS or looking to build one from scratch. As you embrace these new features, you'll experience the benefits of the advancements NodeJS 18 has to offer.
And Finally
As always, I hope you enjoyed this article and got something new. Thank you and see you in the next articles!
If you liked this article, please give me a like and subscribe to support me. Thank you. 😊