-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from nearform/first-review-fix
First review fix
- Loading branch information
Showing
11 changed files
with
294 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# a11-timers | ||
|
||
- Open the file `test/index.test.js`. | ||
|
||
- Use the [Timers API](https://nodejs.org/api/test.html#timers) to mock the `setTimeout` | ||
|
||
- Run in the terminal `node --test`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"name": "a11-timers", | ||
"version": "1.0.0", | ||
"description": "", | ||
"type": "module", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "node --test", | ||
"solution": "node --test ./test/solution.test.js" | ||
}, | ||
"keywords": [], | ||
"author": "", | ||
"license": "ISC" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export function delayedHello(callback, delay) { | ||
setTimeout(() => { | ||
callback('Hello, World!') | ||
}, delay) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/* eslint-disable no-unused-vars */ | ||
import assert from 'node:assert' | ||
import { delayedHello } from '../src/index.js' | ||
import { test, mock } from 'node:test' | ||
|
||
test('delayedHello executes the callback after the specified delay', () => { | ||
const fn = mock.fn() | ||
|
||
// Enable mocking of setTimeout | ||
|
||
// Call the function with a mock callback and a delay | ||
//delayedHello(fn, 5000) | ||
|
||
// Initially, the callback has not been called | ||
//assert.strictEqual(fn.mock.calls.length, 0) | ||
|
||
// Advance time by 5000 milliseconds | ||
|
||
// Now, the callback should have been called once | ||
//assert.strictEqual(fn.mock.calls.length, 1) | ||
//assert.strictEqual(fn.mock.calls[0][0], 'Hello, World!') | ||
|
||
// Reset mock timers after the test | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import assert from 'node:assert' | ||
import { delayedHello } from '../src/index.js' | ||
import { test, mock } from 'node:test' | ||
|
||
test('delayedHello executes the callback after the specified delay', () => { | ||
const fn = mock.fn() | ||
|
||
mock.timers.enable({ apis: ['setTimeout'] }) | ||
delayedHello(fn, 5000) | ||
|
||
// Initially, the callback has not been called | ||
assert.strictEqual(fn.mock.calls.length, 0) | ||
// Advance time by 5000 milliseconds | ||
mock.timers.tick(5000) | ||
// Now, the callback should have been called once | ||
assert.strictEqual(fn.mock.calls.length, 1) | ||
assert.strictEqual(fn.mock.calls[0].arguments[0], 'Hello, World!') | ||
|
||
mock.timers.reset() | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# a12-context | ||
|
||
- Open the file `test/index.test.js`. | ||
|
||
- Group all the subtests of a specific function with a context | ||
|
||
- Run in the terminal `node --test`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"name": "a12-context", | ||
"version": "1.0.0", | ||
"description": "", | ||
"type": "module", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "node --test", | ||
"solution": "node --test ./test/solution.test.js" | ||
}, | ||
"keywords": [], | ||
"author": "", | ||
"license": "ISC" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
export function sum(numbers) { | ||
if (!Array.isArray(numbers)) { | ||
throw new Error('Input must be an array of numbers') | ||
} | ||
|
||
return numbers.reduce((acc, num) => acc + num, 0) | ||
} | ||
|
||
export function average(numbers) { | ||
if (!Array.isArray(numbers)) { | ||
throw new Error('Input must be an array of numbers') | ||
} | ||
|
||
if (numbers.length === 0) { | ||
return 0 | ||
} | ||
|
||
const sum = numbers.reduce((acc, num) => acc + num, 0) | ||
return sum / numbers.length | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { average, sum } from '../src/index.js' | ||
import { test } from 'node:test' | ||
import assert from 'node:assert' | ||
|
||
test('Sum works correctly with valid input', () => { | ||
assert.deepStrictEqual(sum([1, 2, 3]), 6) | ||
}) | ||
|
||
test('Sum returns 0 in case of empty array', () => { | ||
assert.deepStrictEqual(sum([]), 0) | ||
}) | ||
|
||
test('Sum throws in case of bad input', () => { | ||
assert.throws(() => sum('abc'), { | ||
message: 'Input must be an array of numbers' | ||
}) | ||
}) | ||
|
||
test('Average works correctly with valid input', () => { | ||
assert.deepStrictEqual(average([1, 2, 3]), 2) | ||
}) | ||
|
||
test('Average returns 0 in case of empty array', () => { | ||
assert.deepStrictEqual(average([]), 0) | ||
}) | ||
|
||
test('Average throws in case of bad input', () => { | ||
assert.throws(() => average('abc'), { | ||
message: 'Input must be an array of numbers' | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { average, sum } from '../src/index.js' | ||
import { test } from 'node:test' | ||
import assert from 'node:assert' | ||
|
||
// Grouping tests for `sum` function | ||
test('sum function tests', async t => { | ||
await t.test('Sum works correctly with valid input', () => { | ||
assert.deepStrictEqual(sum([1, 2, 3]), 6) | ||
}) | ||
|
||
await t.test('Sum returns 0 in case of empty array', () => { | ||
assert.deepStrictEqual(sum([]), 0) | ||
}) | ||
|
||
await t.test('Sum throws in case of bad input', () => { | ||
assert.throws(() => sum('abc'), { | ||
message: 'Input must be an array of numbers' | ||
}) | ||
}) | ||
}) | ||
|
||
// Grouping tests for `average` function | ||
test('average function tests', async t => { | ||
await t.test('Average works correctly with valid input', () => { | ||
assert.deepStrictEqual(average([1, 2, 3]), 2) | ||
}) | ||
|
||
await t.test('Average returns 0 in case of empty array', () => { | ||
assert.deepStrictEqual(average([]), 0) | ||
}) | ||
|
||
await t.test('Average throws in case of bad input', () => { | ||
assert.throws(() => average('abc'), { | ||
message: 'Input must be an array of numbers' | ||
}) | ||
}) | ||
}) |