This file contains hidden or 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
test('Default value expressions', (t) => { | |
t.plan(3) | |
function bar(val) { | |
return y + val; | |
} | |
function foo(x = y + 3, z = bar( x )) { | |
return [x, z]; |
This file contains hidden or 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
test('es6 way of setting default parameters', (t) => { | |
t.plan(3) | |
function foo(x = 11, y = 31) { | |
return x + y; | |
} | |
let b = foo(5,6); | |
let c = foo(); |
This file contains hidden or 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
import test from 'ava'; | |
test('es5 way of setting default parameters', (t) => { | |
t.plan(3) | |
function foo(x, y) { | |
x = x || 11; | |
y = y || 31; | |
return x + y; |
This file contains hidden or 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
test('rest operator to gather all arguments', (t) => { | |
function foo(...args) { | |
t.same(args, [1,2,3,4,5]); //Compare args value to an array [1,2,3,4,5] | |
} | |
foo(1,2,3,4,5); | |
}); |
This file contains hidden or 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
test('basic spread function with ... operator', (t) => { | |
t.plan(3); | |
function foo(x,y,z) { | |
t.is(x, 1); // true | |
t.is(y, 2); // true | |
t.is(z, 3); // true | |
} | |
foo(...[1,2,3]); |
This file contains hidden or 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
test('rest with multiple arguments', (t) => { | |
function foo(a, b, ...c) { | |
t.is(a, 1); | |
t.is(b, 2); | |
t.same(c, [3,4,5]); | |
} | |
foo(1,2,3,4,5); |
This file contains hidden or 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
test('Block scope functions', (t) => { | |
t.plan(1); | |
{ | |
foo(); | |
function foo() { | |
t.pass(); | |
} | |
} |
This file contains hidden or 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
test('Mutate variables inside complex const variable', (t) => { | |
t.plan(1); | |
{ | |
const a = [1,2,3,4]; | |
a.push(5); | |
t.is(a, [1,2,3,4,5]); | |
} |
This file contains hidden or 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
test('Const variable before and after mutation attempt', (t) => { | |
// Expect two assertions | |
t.plan(2); | |
// Declare thee variables using let | |
const a = 2; | |
t.is(a, 2); | |
try { | |
a = 3; // immutable variable | |
} catch (e) { |
This file contains hidden or 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
test('Accessing let declared variable before being assigned', (t) => { | |
// Expect two assertions | |
t.plan(2); | |
// Declare thee variables using let | |
{ | |
t.is(a, undefined); | |
t.is(b, undefined); | |
var a; |