Skip to content

Commit

Permalink
autoclean optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewplummer committed Jan 27, 2025
1 parent 2436b69 commit 7b93651
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions services/api/src/utils/testing/setup/autoclean.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,8 @@ function autoclean(schema) {
});

schema.post('save', function () {
if (!isFixture(this)) {
if (this.destroy) {
stored.add(this);
}
if (canAddToStored(this)) {
stored.set(this.id, this);
}
});

Expand Down Expand Up @@ -59,14 +57,21 @@ function autoclean(schema) {
});
}

function canAddToStored(doc) {
if (isFixture(doc) || !doc.destroy) {
return false;
}
return !stored.has(doc.id);
}

beforeEach(async () => {
stored = new Set();
stored = new Map();
});

afterEach(async () => {
if (stored) {
await Promise.all(
Array.from(stored).map((doc) => {
Array.from(stored.values()).map((doc) => {
return doc.destroy();
})
);
Expand Down

0 comments on commit 7b93651

Please sign in to comment.