From 76a31c317533c87c03fe0ca1970e8286cc6b4fcf Mon Sep 17 00:00:00 2001 From: Whymarrh Whitby Date: Wed, 15 Jan 2020 18:24:28 -0330 Subject: [PATCH] Clean up Migrator test cases (#7835) --- app/scripts/background.js | 1 + app/scripts/lib/migrator/index.js | 1 - test/unit/migrations/migrator-test.js | 40 ++++++++++++--------------- 3 files changed, 18 insertions(+), 24 deletions(-) diff --git a/app/scripts/background.js b/app/scripts/background.js index 20e05039a..7f1c13686 100644 --- a/app/scripts/background.js +++ b/app/scripts/background.js @@ -167,6 +167,7 @@ async function initialize () { async function loadStateFromPersistence () { // migrations const migrator = new Migrator({ migrations }) + migrator.on('error', console.warn) // read from disk // first from preferred, async API: diff --git a/app/scripts/lib/migrator/index.js b/app/scripts/lib/migrator/index.js index 8bd6f9217..73526bbf8 100644 --- a/app/scripts/lib/migrator/index.js +++ b/app/scripts/lib/migrator/index.js @@ -52,7 +52,6 @@ class Migrator extends EventEmitter { // rewrite error message to add context without clobbering stack const originalErrorMessage = err.message err.message = `MetaMask Migration Error #${migration.version}: ${originalErrorMessage}` - console.warn(err.stack) // emit error instead of throw so as to not break the run (gracefully fail) this.emit('error', err) // stop migrating and use state as is diff --git a/test/unit/migrations/migrator-test.js b/test/unit/migrations/migrator-test.js index bd80ff957..5173f8551 100644 --- a/test/unit/migrations/migrator-test.js +++ b/test/unit/migrations/migrator-test.js @@ -60,33 +60,27 @@ describe('liveMigrations require list', () => { describe('Migrator', () => { const migrator = new Migrator({ migrations: stubMigrations }) - it('migratedData version should be version 3', (done) => { - migrator.migrateData(versionedData) - .then((migratedData) => { - assert.equal(migratedData.meta.version, stubMigrations[2].version) - done() - }).catch(done) + it('migratedData version should be version 3', async () => { + const migratedData = await migrator.migrateData(versionedData) + assert.equal(migratedData.meta.version, stubMigrations[2].version) }) - it('should match the last version in live migrations', (done) => { + it('should match the last version in live migrations', async () => { const migrator = new Migrator({ migrations: liveMigrations }) - migrator.migrateData(firstTimeState) - .then((migratedData) => { - const last = liveMigrations.length - 1 - assert.equal(migratedData.meta.version, liveMigrations[last].version) - done() - }).catch(done) + const migratedData = await migrator.migrateData(firstTimeState) + const last = liveMigrations.length - 1 + assert.equal(migratedData.meta.version, liveMigrations[last].version) }) - it('should emit an error', function (done) { - this.timeout(15000) - const migrator = new Migrator({ migrations: [{ version: 1, migrate: async () => { - throw new Error('test') - } } ] }) - migrator.on('error', () => done()) - migrator.migrateData({ meta: { version: 0 } }) - .then(() => { - }).catch(done) + it('should emit an error', async () => { + const migrator = new Migrator({ + migrations: [{ + version: 1, + async migrate () { + throw new Error('test') + }, + }], + }) + await assert.rejects(migrator.migrateData({ meta: { version: 0 } })) }) - })