Skip to content

Commit

Permalink
test: 💍 add end to end test
Browse files Browse the repository at this point in the history
  • Loading branch information
shufo committed Feb 3, 2021
1 parent e2540d0 commit 69b6a4c
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 21 deletions.
39 changes: 24 additions & 15 deletions test/runTest.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,32 @@
const path = require('path');
const path = require("path");

const { runTests } = require('vscode-test');
const { runTests } = require("vscode-test");

async function main() {
try {
// The folder containing the Extension Manifest package.json
// Passed to `--extensionDevelopmentPath`
const extensionDevelopmentPath = path.resolve(__dirname, '../');
try {
// The folder containing the Extension Manifest package.json
// Passed to `--extensionDevelopmentPath`
const extensionDevelopmentPath = path.resolve(__dirname, "../");

// The path to the extension test script
// Passed to --extensionTestsPath
const extensionTestsPath = path.resolve(__dirname, './suite/index');
// The path to the extension test script
// Passed to --extensionTestsPath
const extensionTestsPath = path.resolve(__dirname, "./suite/index");

// Download VS Code, unzip it and run the integration test
await runTests({ extensionDevelopmentPath, extensionTestsPath });
} catch (err) {
console.error('Failed to run tests');
process.exit(1);
}
const launchArgs = [
`${path.resolve(__dirname, "../")}/test/fixtures/test.code-workspace`,
"--disable-extensions",
];

// Download VS Code, unzip it and run the integration test
await runTests({
extensionDevelopmentPath,
extensionTestsPath,
launchArgs,
});
} catch (err) {
console.error("Failed to run tests");
process.exit(1);
}
}

main();
92 changes: 86 additions & 6 deletions test/suite/extension.test.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,93 @@
const assert = require('assert');
const assert = require("assert");
const path = require("path");
const fs = require("fs");

// You can import and use all API from the 'vscode' module
// as well as import your extension to test it
const vscode = require('vscode');
const vscode = require("vscode");
const { before } = require("mocha");
// const myExtension = require('../extension');

suite('Extension Test Suite', () => {
vscode.window.showInformationMessage('Start all tests.');
suite("Extension Test Suite", () => {
vscode.window.showInformationMessage("Start all tests.");

test('Sample test', () => {
});
test("Should format file with extension", async function () {
this.timeout(20000);
await formatSameAsBladeFormatter(
"index.blade.php",
"formatted.index.blade.php"
);
});

test("Should ignore file if target listed in .bladeignore", async function () {
this.timeout(20000);
await formatSameAsBladeFormatter("ignore.blade.php", "ignore.blade.php");
});
});

async function formatSameAsBladeFormatter(file, formattedFile, options) {
const { actual, source } = await format("project", file);
const formatted = await getContent("project", formattedFile);
assert.equal(actual, formatted);
}

async function getContent(workspaceFolderName, testFile) {
const base = getWorkspaceFolderUri(workspaceFolderName);
const absPath = path.join(base.fsPath, `${testFile}`);
return fs.readFileSync(absPath).toString("utf-8");
}

async function format(workspaceFolderName, testFile) {
const base = getWorkspaceFolderUri(workspaceFolderName);
const absPath = path.join(base.fsPath, testFile);
const doc = await vscode.workspace.openTextDocument(absPath);
const text = await doc.getText();
try {
await vscode.window.showTextDocument(doc);
} catch (error) {
// eslint-disable-next-line no-console
console.log(error);
throw error;
}
// eslint-disable-next-line no-console
console.time(testFile);
await waitForActivation("shufo.vscode-blade-formatter");
for (let i = 0; i < 3; i++) {
await vscode.commands.executeCommand("editor.action.formatDocument");
}

// eslint-disable-next-line no-console
console.timeEnd(testFile);

return { actual: await doc.getText(), source: text };
}

function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}

async function waitForActivation(extensionId) {
let i = 0;
while (vscode.extensions.getExtension(extensionId).isActive === false) {
if (i === 10) {
break;
}

await sleep(1000);
i++;
}

return true;
}

const getWorkspaceFolderUri = (workspaceFolderName) => {
const workspaceFolder = vscode.workspace.workspaceFolders.find((folder) => {
return folder.name === workspaceFolderName;
});
if (!workspaceFolder) {
throw new Error(
"Folder not found in workspace. Did you forget to add the test folder to test.code-workspace?"
);
}
return workspaceFolder.uri;
};

0 comments on commit 69b6a4c

Please sign in to comment.