diff --git a/spec/headers_test.js b/spec/headers_test.js index 44eb2ed1538ac76ed32d50ff6b12faac68dde334..93f572cde9118eab366ab2c90f54d9c8a18864ee 100644 --- a/spec/headers_test.js +++ b/spec/headers_test.js @@ -52,7 +52,7 @@ describe('Responses contain custom headers', () => { it('index.html has version', async () => { const response = await request(app).get('/index.html') expect(response.statusCode).toBe(200) - expect(response.headers.version).toMatch(/\d*\.3220550168/) + expect(response.headers.version).toMatch(/\d*\.\d*/) }) it('javascript file contains dependencies', async () => { diff --git a/spec/util_test.js b/spec/util_test.js new file mode 100644 index 0000000000000000000000000000000000000000..eb2bdda9db770a0d1312af7fe18d5c43354422d0 --- /dev/null +++ b/spec/util_test.js @@ -0,0 +1,30 @@ +import { describe, it, expect } from '@jest/globals' +import { hash } from '../src/util' +import { generateSimpleViteManifest } from './util' + +describe('Util', function () { + describe('hash function', function () { + it('computes the hash', function () { + const manifest = generateSimpleViteManifest({ + 'example.js': { imports: ['test.css'] }, + 'test.css': { } + }) + expect(hash(manifest)).toEqual('2245696177') + }) + + it('the hash changes when the manifest changes', function () { + const manifest = generateSimpleViteManifest({ + 'example.js': { imports: ['test.css'] }, + 'test.css': { } + }) + const manifestChanged = generateSimpleViteManifest({ + 'example.js': { imports: ['test1.css'] }, + 'test1.css': { } + }) + + expect(hash(manifest)).not.toEqual(hash(manifestChanged)) + expect(hash(manifest)).toEqual('2245696177') + expect(hash(manifestChanged)).toEqual('2547998666') + }) + }) +}) diff --git a/src/util.js b/src/util.js index 0229cba7f435a4cd3554dacc1452e07a9b78ef48..2238880b6b439be19102c00aaa3c48d7a3548742 100644 --- a/src/util.js +++ b/src/util.js @@ -1,6 +1,6 @@ // totaly awesome hash function. Do not use this for encryption (crypto.subtle.digest etc would be overkill for this) export function hash (array) { - const string = array.toString() + const string = JSON.stringify(array) if (!string.length) throw new Error('TypeError: Unexpected data to calculate hash from') let hash = 0