|
3 | 3 | import * as Eta from '../src/index'
|
4 | 4 | import { buildRegEx } from './err.spec'
|
5 | 5 |
|
6 |
| -function resolveAfter2Seconds(val: string): Promise<string> { |
| 6 | +type AsyncTimes = {time: number, type: "start" | "end"}[] |
| 7 | + |
| 8 | +function resolveAfter20Milliseconds(val: string): Promise<string> { |
7 | 9 | return new Promise((resolve) => {
|
8 | 10 | setTimeout(() => {
|
9 | 11 | resolve(val)
|
10 | 12 | }, 20)
|
11 | 13 | })
|
12 | 14 | }
|
13 | 15 |
|
| 16 | +function createWaitAndStoreTimes(times: AsyncTimes): (val: string) => Promise<string> { |
| 17 | + return val => new Promise<string>((resolve) => { |
| 18 | + times.push({time: Date.now(), type: 'start'}); |
| 19 | + setTimeout(() => { |
| 20 | + resolve(val) |
| 21 | + }, 200) |
| 22 | + }).then(x => { |
| 23 | + times.push({time: Date.now(), type: 'end'}); |
| 24 | + return x; |
| 25 | + }) |
| 26 | +} |
| 27 | + |
14 | 28 | async function asyncTest() {
|
15 |
| - const result = await resolveAfter2Seconds('HI FROM ASYNC') |
| 29 | + const result = await resolveAfter20Milliseconds('HI FROM ASYNC') |
16 | 30 | return result
|
17 | 31 | }
|
18 | 32 |
|
@@ -56,6 +70,59 @@ if(cb){cb(null,tR)} return tR
|
56 | 70 | )
|
57 | 71 | })
|
58 | 72 |
|
| 73 | + it('does not await code in eval tags with globalAwait option', async () => { |
| 74 | + expect( |
| 75 | + await Eta.render('Hi <% if (it.name) { %>0<% } %>', { name: Promise.resolve(false) }, { async: true, globalAwait: true }) |
| 76 | + ).toEqual('Hi 0') |
| 77 | + }) |
| 78 | + |
| 79 | + it('automatically awaits promises with the globalAwait option', async () => { |
| 80 | + expect( |
| 81 | + await Eta.render('Hi <%= it.name %>', { name: Promise.resolve('Ada Lovelace') }, { async: true, globalAwait: true }) |
| 82 | + ).toEqual('Hi Ada Lovelace') |
| 83 | + }) |
| 84 | + |
| 85 | + it('globalAwait option works with an empty interpolation tag', async () => { |
| 86 | + expect( |
| 87 | + await Eta.render('Hi <%=%>', { }, { async: true, globalAwait: true }) |
| 88 | + ).toEqual('Hi undefined') |
| 89 | + }) |
| 90 | + |
| 91 | + it('awaits all promises in parallel with the globalAwait option (escaped)', async () => { |
| 92 | + const times: AsyncTimes = []; |
| 93 | + const wait = createWaitAndStoreTimes(times) |
| 94 | + const result = await Eta.render('Hi <%= it.wait(1) %> <%= it.wait(2) %> <%= it.wait(3) %>', { wait }, { async: true, globalAwait: true }) |
| 95 | + expect(result).toEqual('Hi 1 2 3') |
| 96 | + times.sort((t1, t2) => t1.time < t2.time ? -1 : t1.time > t2.time ? 1 : 0) |
| 97 | + expect(times.map(t => t.type).join(',')).toBe('start,start,start,end,end,end') |
| 98 | + }) |
| 99 | + |
| 100 | + it('awaits all promises in sequence without the globalAwait option (escaped)', async () => { |
| 101 | + const times: AsyncTimes = []; |
| 102 | + const wait = createWaitAndStoreTimes(times) |
| 103 | + const result = await Eta.render('Hi <%= await it.wait(1) %> <%= await it.wait(2) %> <%= await it.wait(3) %>', { wait }, { async: true, globalAwait: false }) |
| 104 | + expect(result).toEqual('Hi 1 2 3') |
| 105 | + times.sort((t1, t2) => t1.time < t2.time ? -1 : t1.time > t2.time ? 1 : 0) |
| 106 | + expect(times.map(t => t.type).join(',')).toBe('start,end,start,end,start,end') |
| 107 | + }) |
| 108 | + |
| 109 | + it('awaits all promises in parallel with the globalAwait option (raw)', async () => { |
| 110 | + const times: AsyncTimes = []; |
| 111 | + const wait = createWaitAndStoreTimes(times) |
| 112 | + const result = await Eta.render('Hi <%~ it.wait(1) %> <%~ it.wait(2) %> <%~ it.wait(3) %>', { wait }, { async: true, globalAwait: true }) |
| 113 | + expect(result).toEqual('Hi 1 2 3') |
| 114 | + times.sort((t1, t2) => t1.time < t2.time ? -1 : t1.time > t2.time ? 1 : 0) |
| 115 | + expect(times.map(t => t.type).join(',')).toBe('start,start,start,end,end,end') |
| 116 | + }) |
| 117 | + |
| 118 | + it('awaits all promises in sequence without the globalAwait option (raw)', async () => { |
| 119 | + const times: AsyncTimes = []; |
| 120 | + const wait = createWaitAndStoreTimes(times) |
| 121 | + const result = await Eta.render('Hi <%~ await it.wait(1) %> <%~ await it.wait(2) %> <%~ await it.wait(3) %>', { wait }, { async: true, globalAwait: false }) |
| 122 | + expect(result).toEqual('Hi 1 2 3') |
| 123 | + times.sort((t1, t2) => t1.time < t2.time ? -1 : t1.time > t2.time ? 1 : 0) |
| 124 | + expect(times.map(t => t.type).join(',')).toBe('start,end,start,end,start,end') |
| 125 | + }) |
59 | 126 | it('Async template w/ syntax error passes error to callback', (done) => {
|
60 | 127 | function cb(err: Error | null, _res?: string) {
|
61 | 128 | expect(err).toBeTruthy()
|
|
0 commit comments