-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Cover extractSnippets() with basic tests.
- Loading branch information
1 parent
0db7bff
commit a2f9e1e
Showing
1 changed file
with
71 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
const expect = require('unexpected'); | ||
|
||
const evaluateSnippets = require('../lib/evaluateSnippets'); | ||
|
||
describe('extractSnippets', () => { | ||
it('should evaluate javascript snippets', async () => { | ||
const snippets = [ | ||
{ | ||
lang: 'javascript', | ||
flags: { evaluate: true }, | ||
index: 24, | ||
code: | ||
'throw new Error("foo\\n at bar (/somewhere.js:1:2)\\n at quux (/blah.js:3:4)\\n at baz (/yadda.js:5:6)")' | ||
}, | ||
{ | ||
lang: 'output', | ||
flags: { cleanStackTrace: true, evaluate: true }, | ||
index: 198, | ||
code: | ||
'foo\n at bar (/path/to/file.js:x:y)\n at quux (/path/to/file.js:x:y)' | ||
} | ||
]; | ||
|
||
await evaluateSnippets(snippets); | ||
|
||
expect(snippets[0], 'to satisfy', { | ||
htmlErrorMessage: | ||
'<div style="font-family: monospace; white-space: nowrap"><div><span style="color: red; font-weight: bold">foo</span></div><div><span style="color: red; font-weight: bold"> at bar (/somewhere.js:1:2)</span></div><div><span style="color: red; font-weight: bold"> at quux (/blah.js:3:4)</span></div><div><span style="color: red; font-weight: bold"> at baz (/yadda.js:5:6)</span></div></div>', | ||
errorMessage: | ||
'foo\n at bar (/somewhere.js:1:2)\n at quux (/blah.js:3:4)\n at baz (/yadda.js:5:6)' | ||
}); | ||
}); | ||
|
||
describe('with an aync snippet', () => { | ||
it('should evaluate javascript snippets', async () => { | ||
const snippets = [ | ||
{ | ||
lang: 'javascript', | ||
flags: { async: true, evaluate: true }, | ||
index: 40, | ||
code: "return Promise.reject(new Error('boom'));" | ||
} | ||
]; | ||
|
||
await evaluateSnippets(snippets); | ||
|
||
expect(snippets[0], 'to satisfy', { | ||
htmlErrorMessage: '<div style="font-family: monospace; white-space: nowrap"><div><span style="color: red; font-weight: bold">boom</span></div></div>', | ||
errorMessage: 'boom' | ||
}); | ||
}); | ||
|
||
it('should record an error string if missing a return', async () => { | ||
const snippets = [ | ||
{ | ||
lang: 'javascript', | ||
flags: { async: true, evaluate: true }, | ||
index: 40, | ||
code: "Promise.resolve();" | ||
} | ||
]; | ||
|
||
await evaluateSnippets(snippets); | ||
|
||
expect(snippets[0], 'to satisfy', { | ||
htmlErrorMessage: '<div style="font-family: monospace; white-space: nowrap"><div><span style="color: red; font-weight: bold">Async code block did not return a promise or throw</span></div><div><span style="color: red; font-weight: bold">Promise.resolve();</span></div></div>', | ||
errorMessage: 'Async code block did not return a promise or throw\nPromise.resolve();' | ||
}); | ||
}); | ||
}); | ||
}); |