Skip to content

Commit

Permalink
test: Re-arranged the tests to move them to the core tests directory
Browse files Browse the repository at this point in the history
  • Loading branch information
foxxor committed Feb 11, 2021
1 parent d199b19 commit 477b9d6
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 66 deletions.
File renamed without changes.
103 changes: 103 additions & 0 deletions test/functional/commands/migration-create.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import sinon from "sinon";
import { ConnectionOptions, ConnectionOptionsReader, DatabaseType } from "../../../src";
import { setupTestingConnections, createTestingConnections, closeTestingConnections, reloadTestingDatabases } from "../../utils/test-utils";
import { CommandUtils } from "../../../src/commands/CommandUtils";
import { MigrationCreateCommand } from "../../../src/commands/MigrationCreateCommand";
import { Post } from "./entity/Post";
import { resultsTemplates } from "./templates/result-templates-create";

describe("github issues > #7253 Allow migration files to be output in Javascript", () => {
let connectionOptions: ConnectionOptions[];
let createFileStub: sinon.SinonStub;
let timerStub: sinon.SinonFakeTimers;
let getConnectionOptionsStub: sinon.SinonStub;
let migrationCreateCommand: MigrationCreateCommand;
let connectionOptionsReader: ConnectionOptionsReader;
let baseConnectionOptions: ConnectionOptions;

const enabledDrivers = [
"mysql",
] as DatabaseType[];

// simulate args: `npm run typeorm migration:run -- -n test-migration -d test-directory`
const testHandlerArgs = (options: Record<string, any>) => ({
"$0": "test",
"_": ["test"],
"name": "test-migration",
"dir": "test-directory",
...options
});

before(async () => {
// clean out db from any prior tests in case previous state impacts the generated migrations
const connections = await createTestingConnections({
entities: [],
enabledDrivers
});
await reloadTestingDatabases(connections);
await closeTestingConnections(connections);

connectionOptions = setupTestingConnections({
entities: [Post],
enabledDrivers
});
connectionOptionsReader = new ConnectionOptionsReader();
baseConnectionOptions = await connectionOptionsReader.get(connectionOptions[0].name as string);

migrationCreateCommand = new MigrationCreateCommand();
createFileStub = sinon.stub(CommandUtils, "createFile");

timerStub = sinon.useFakeTimers(1610975184784);
});

after(async () => {
timerStub.restore();
createFileStub.restore();
});

beforeEach(async () => {
getConnectionOptionsStub = sinon.stub(ConnectionOptionsReader.prototype, "get").resolves({
...baseConnectionOptions,
entities: [Post]
});
});

afterEach(async () => {
getConnectionOptionsStub.restore();
});

it("writes regular empty migration file when no option is passed", async () => {
createFileStub.resetHistory();

await migrationCreateCommand.handler(testHandlerArgs({
"connection": connectionOptions[0].name
}));

// compare against control test strings in results-templates.ts
sinon.assert.calledWith(
createFileStub,
sinon.match(/test-directory.*test-migration.ts/),
sinon.match(resultsTemplates.create.control)
);

getConnectionOptionsStub.restore();
});

it("writes Javascript empty migration file when option is passed", async () => {
createFileStub.resetHistory();

await migrationCreateCommand.handler(testHandlerArgs({
"connection": connectionOptions[0].name,
"outputJs": true
}));

// compare against control test strings in results-templates.ts
sinon.assert.calledWith(
createFileStub,
sinon.match(/test-directory.*test-migration.js/),
sinon.match(resultsTemplates.create.javascript)
);

getConnectionOptionsStub.restore();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,15 @@ import { ConnectionOptions, ConnectionOptionsReader, DatabaseType } from "../../
import { setupTestingConnections, createTestingConnections, closeTestingConnections, reloadTestingDatabases } from "../../utils/test-utils";
import { CommandUtils } from "../../../src/commands/CommandUtils";
import { MigrationGenerateCommand } from "../../../src/commands/MigrationGenerateCommand";
import { MigrationCreateCommand } from "../../../src/commands/MigrationCreateCommand";
import { Post } from "./entity/Post";
import { resultsTemplates } from "./results-templates";
import { resultsTemplates } from "./templates/result-templates-generate";

describe("github issues > #7253 Allow migration files to be output in Javascript", () => {
let connectionOptions: ConnectionOptions[];
let createFileStub: sinon.SinonStub;
let timerStub: sinon.SinonFakeTimers;
let getConnectionOptionsStub: sinon.SinonStub;
let migrationGenerateCommand: MigrationGenerateCommand;
let migrationCreateCommand: MigrationCreateCommand;
let connectionOptionsReader: ConnectionOptionsReader;
let baseConnectionOptions: ConnectionOptions;

Expand Down Expand Up @@ -47,7 +45,6 @@ describe("github issues > #7253 Allow migration files to be output in Javascript
baseConnectionOptions = await connectionOptionsReader.get(connectionOptions[0].name as string);

migrationGenerateCommand = new MigrationGenerateCommand();
migrationCreateCommand = new MigrationCreateCommand();
createFileStub = sinon.stub(CommandUtils, "createFile");

timerStub = sinon.useFakeTimers(1610975184784);
Expand All @@ -69,41 +66,6 @@ describe("github issues > #7253 Allow migration files to be output in Javascript
getConnectionOptionsStub.restore();
});

it("writes regular empty migration file when no option is passed", async () => {
createFileStub.resetHistory();

await migrationCreateCommand.handler(testHandlerArgs({
"connection": connectionOptions[0].name
}));

// compare against control test strings in results-templates.ts
sinon.assert.calledWith(
createFileStub,
sinon.match(/test-directory.*test-migration.ts/),
sinon.match(resultsTemplates.create.control)
);

getConnectionOptionsStub.restore();
});

it("writes Javascript empty migration file when option is passed", async () => {
createFileStub.resetHistory();

await migrationCreateCommand.handler(testHandlerArgs({
"connection": connectionOptions[0].name,
"outputJs": true
}));

// compare against control test strings in results-templates.ts
sinon.assert.calledWith(
createFileStub,
sinon.match(/test-directory.*test-migration.js/),
sinon.match(resultsTemplates.create.javascript)
);

getConnectionOptionsStub.restore();
});

it("writes regular migration file when no option is passed", async () => {
createFileStub.resetHistory();

Expand Down
23 changes: 23 additions & 0 deletions test/functional/commands/templates/result-templates-create.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
export const resultsTemplates: Record<string, any> = {
control: `import {MigrationInterface, QueryRunner} from "typeorm";
export class testMigration1610975184784 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<void> {
}
public async down(queryRunner: QueryRunner): Promise<void> {
}
}`,
javascript: `const { MigrationInterface, QueryRunner } = require("typeorm");
module.exports = class testMigration1610975184784 {
async up(queryRunner) {
}
async down(queryRunner) {
}
}`
};
Original file line number Diff line number Diff line change
@@ -1,29 +1,5 @@
export const resultsTemplates: Record<string, any> = {
create: {
control: `import {MigrationInterface, QueryRunner} from "typeorm";
export class testMigration1610975184784 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<void> {
}
public async down(queryRunner: QueryRunner): Promise<void> {
}
}`,
javascript: `const { MigrationInterface, QueryRunner } = require("typeorm");
module.exports = class testMigration1610975184784 {
async up(queryRunner) {
}
async down(queryRunner) {
}
}`
},
generate: {
control: `import {MigrationInterface, QueryRunner} from "typeorm";
control: `import {MigrationInterface, QueryRunner} from "typeorm";
export class testMigration1610975184784 implements MigrationInterface {
name = 'testMigration1610975184784'
Expand All @@ -37,7 +13,7 @@ export class testMigration1610975184784 implements MigrationInterface {
}
}`,
javascript: `const { MigrationInterface, QueryRunner } = require("typeorm");
javascript: `const { MigrationInterface, QueryRunner } = require("typeorm");
module.exports = class testMigration1610975184784 {
name = 'testMigration1610975184784'
Expand All @@ -50,5 +26,4 @@ module.exports = class testMigration1610975184784 {
await queryRunner.query("DROP TABLE \`post\`");
}
}`
}
};

0 comments on commit 477b9d6

Please sign in to comment.