|
1 | 1 | import chai from 'chai' |
2 | | -import sinon from 'sinon' |
3 | | -import sinonChai from 'sinon-chai' |
4 | 2 |
|
5 | | -chai.use(sinonChai) |
6 | 3 | const { expect } = chai |
7 | 4 |
|
8 | 5 | import * as fs from 'fs' |
9 | 6 | import { getTemplate } from '../../../src/utils/template-cache' |
| 7 | +import { join } from 'path' |
| 8 | +import { tmpdir } from 'os' |
10 | 9 |
|
11 | 10 | describe('getTemplate', () => { |
12 | | - let readFileSyncStub: sinon.SinonStub |
| 11 | + const tmpFile = join(tmpdir(), 'test-template.html') |
13 | 12 |
|
14 | 13 | beforeEach(() => { |
15 | | - readFileSyncStub = sinon.stub(fs, 'readFileSync').returns('template content' as any) |
| 14 | + fs.writeFileSync(tmpFile, 'template content') |
16 | 15 | }) |
17 | 16 |
|
18 | 17 | afterEach(() => { |
19 | | - readFileSyncStub.restore() |
| 18 | + try { fs.unlinkSync(tmpFile) } catch (e) { /* ignore */ } |
20 | 19 | }) |
21 | 20 |
|
22 | 21 | it('returns the file content', () => { |
23 | | - const result = getTemplate('./resources/index.html') |
24 | | - |
| 22 | + const result = getTemplate(tmpFile) |
25 | 23 | expect(result).to.equal('template content') |
26 | 24 | }) |
27 | 25 |
|
28 | | - it('reads the file with utf8 encoding', () => { |
29 | | - getTemplate('./resources/index.html') |
30 | | - |
31 | | - expect(readFileSyncStub).to.have.been.calledWith('./resources/index.html', 'utf8') |
32 | | - }) |
33 | | - |
34 | 26 | it('reads the file on every call in non-production mode', () => { |
35 | 27 | // NODE_ENV is not 'production' in tests — cache is bypassed |
36 | | - getTemplate('./resources/index.html') |
37 | | - getTemplate('./resources/index.html') |
| 28 | + getTemplate(tmpFile) |
| 29 | + fs.writeFileSync(tmpFile, 'updated content') |
| 30 | + const result2 = getTemplate(tmpFile) |
38 | 31 |
|
39 | | - expect(readFileSyncStub).to.have.been.calledTwice |
| 32 | + expect(result2).to.equal('updated content') |
40 | 33 | }) |
41 | 34 |
|
42 | 35 | it('propagates errors thrown by readFileSync', () => { |
43 | | - readFileSyncStub.throws(new Error('file not found')) |
44 | | - |
45 | | - expect(() => getTemplate('./resources/missing.html')).to.throw('file not found') |
| 36 | + expect(() => getTemplate('./does-not-exist.html')).to.throw(/ENOENT/) |
46 | 37 | }) |
47 | 38 | }) |
0 commit comments