Skip to content

Commit

Permalink
setup node testing framework
Browse files Browse the repository at this point in the history
  • Loading branch information
marekzelinka committed Jul 24, 2024
1 parent 58c22b1 commit 4a1c004
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
"scripts": {
"start": "node src/index.js",
"dev": "node --watch --env-file=.env src/index.js",
"test": "echo \"Error: no test specified\" && exit 1",
"lint": "eslint src",
"test": "node --test --watch",
"format": "prettier . -w"
},
"devDependencies": {
Expand Down
11 changes: 11 additions & 0 deletions src/utils/test-demo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export function reverse(string) {
return string.split('').reverse().join('')
}

export function average(array) {
if (!array.length) {
return 0
}

return array.reduce((sum, item) => sum + item, 0) / array.length
}
37 changes: 37 additions & 0 deletions src/utils/test-demo.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import assert from 'node:assert'
import { describe, test } from 'node:test'
import { average, reverse } from './test-demo.js'

describe('reverse', () => {
test('reverse of a', () => {
const actual = reverse('a')
const expected = 'a'
assert.strictEqual(actual, expected)
})

test('reverse of react', () => {
const actual = reverse('react')
const expected = 'tcaer'
assert.strictEqual(actual, expected)
})

test('reverse of saippuakauppias', () => {
const actual = reverse('saippuakauppias')
const expected = 'saippuakauppias'
assert.strictEqual(actual, expected)
})
})

describe('average', () => {
test('of one value is the value itself', () => {
assert.strictEqual(average([1]), 1)
})

test('of many items is calculated right', () => {
assert.strictEqual(average([1, 2, 3, 4, 5, 6]), 3.5)
})

test('of empty array is zero', () => {
assert.strictEqual(average([]), 0)
})
})

0 comments on commit 4a1c004

Please sign in to comment.