Skip to content

Commit

Permalink
add inferring watch mode and not restarting containers (#19)
Browse files Browse the repository at this point in the history
* add inferring watch mode and not restarting containers

* add optional config to disable watch optimization
  • Loading branch information
Yengas authored Dec 2, 2020
1 parent 85c0541 commit c8fb8a4
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@trendyol/jest-testcontainers",
"version": "1.4.0",
"version": "2.0.0",
"description": "Jest preset for starting docker containers that stay up whilist your tests run.",
"main": "dist/index",
"types": "dist/index",
Expand Down
10 changes: 9 additions & 1 deletion src/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,15 @@ function createGlobalVariablesFromMetaInfos(
}, {});
}

async function setup() {
async function setup(opts: any) {
if (
!process.env.JEST_TESTCONTAINERS_RESTART_ON_WATCH &&
(opts.watch || opts.watchAll) &&
global.__TESTCONTAINERS__
) {
return;
}

const jestTestcontainersConfig = configReader();
const allStartedContainersMetaInfo = await startAllContainers(
jestTestcontainersConfig
Expand Down
19 changes: 18 additions & 1 deletion src/teardown.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,29 @@ describe("teardown", () => {
(global as any)[CONTAINERS_GLOBAL_VARIABLE_KEY] = mocks;

// Act
await teardown();
await teardown({});

// Assert
for (const { stop: mockCallback } of mocks) {
expect(mockCallback.mock.calls.length).toBe(1);
}
});

it("should not call stop if started in watch mode", async () => {
// Arrange
const mocks = [...new Array(5)].map(() => ({
stop: jest.fn(() => Promise.resolve())
}));

(global as any)[CONTAINERS_GLOBAL_VARIABLE_KEY] = mocks;

// Act
await teardown({ watch: true });

// Assert
for (const { stop: mockCallback } of mocks) {
expect(mockCallback.mock.calls.length).toBe(0);
}
});
});
});
9 changes: 8 additions & 1 deletion src/teardown.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import { StartedContainerAndMetaInfo } from "./containers";

async function teardown() {
async function teardown(opts: any) {
if (
!process.env.JEST_TESTCONTAINERS_RESTART_ON_WATCH &&
(opts.watch || opts.watchAll)
) {
return;
}

const allStartedContainers: StartedContainerAndMetaInfo[] = (global as any)
.__TESTCONTAINERS__;

Expand Down

0 comments on commit c8fb8a4

Please sign in to comment.