aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--package.json8
-rw-r--r--tests/fixtures/foo.js1
-rw-r--r--tests/formatting.test.js118
3 files changed, 127 insertions, 0 deletions
diff --git a/package.json b/package.json
index ac2ee96..eb53112 100644
--- a/package.json
+++ b/package.json
@@ -8,6 +8,9 @@
"type": "git",
"url": "git://github.com/prettier/vim-prettier.git"
},
+ "scripts": {
+ "test": "LOG_LEVEL=error jest"
+ },
"dependencies": {
"@prettier/plugin-lua": "0.0.1",
"@prettier/plugin-php": "^0.10.2",
@@ -15,5 +18,10 @@
"@prettier/plugin-ruby": "^0.8.0",
"@prettier/plugin-swift": "prettier/plugin-swift",
"prettier": "^1.16.4"
+ },
+ "devDependencies": {
+ "colors": "^1.3.2",
+ "jest": "^23.6.0",
+ "vim-driver": "^1.0.0"
}
}
diff --git a/tests/fixtures/foo.js b/tests/fixtures/foo.js
new file mode 100644
index 0000000..82955ab
--- /dev/null
+++ b/tests/fixtures/foo.js
@@ -0,0 +1 @@
+const a = () => { return 2; };
diff --git a/tests/formatting.test.js b/tests/formatting.test.js
new file mode 100644
index 0000000..d627921
--- /dev/null
+++ b/tests/formatting.test.js
@@ -0,0 +1,118 @@
+const fs = require('fs');
+const path = require('path');
+const HeadlessRemoteClient = require('vim-driver/dist/HeadlessRemoteClient');
+const Server = require('vim-driver/dist/Server');
+
+const HOST = '127.0.0.1';
+const PORT = 1337;
+const FIXTURES_DIR = `${__dirname}/fixtures`;
+
+let server;
+let remote;
+
+const getBufferContents = async remote =>
+ (await remote.call('getline', [1, '$'])).join('\n');
+
+const sleep = ms => new Promise(resolve => setTimeout(resolve, ms));
+const waitUntil = (condition, timeout = 4000) => {
+ return new Promise(resolve => {
+ let isTimedOut = false;
+ let timeoutId = null;
+
+ const check = () => {
+ const promise = condition();
+ promise.then(result => {
+ if (!isTimedOut && result === true) {
+ clearTimeout(timeoutId);
+ resolve();
+ } else if (!isTimedOut) {
+ check();
+ }
+ });
+ };
+
+ timeoutId = setTimeout(() => {
+ isTimedOut = true;
+ resolve();
+ }, timeout);
+ });
+};
+
+const assertFormatting = (file) => {
+ const filename = path.basename(file);
+ test(`Prettier formats ${filename} file with :Prettier command`, async () => {
+ await remote.edit(`${FIXTURES_DIR}/${file}`);
+
+ const lines = await getBufferContents(remote);
+
+ // run sync formatting
+ await remote.execute('Prettier');
+
+ const updatedLines = await getBufferContents(remote);
+
+ // we now check that we have indeed formatted the code
+ expect(updatedLines).not.toBe(lines);
+ });
+
+ test(`Prettier formats ${filename} file with :PrettierAsync command`, async () => {
+ await remote.edit(`${FIXTURES_DIR}/${file}`);
+
+ const lines = await getBufferContents(remote);
+
+ // run async formatting
+ await remote.execute('PrettierAsync');
+
+ const unmodifiedLines = await getBufferContents(remote);
+
+ // async should not happen immediatly so content should still remain the same
+ expect(unmodifiedLines).toBe(lines);
+
+ // we now will wait until prettier has finally updated the content async
+ await waitUntil(async () => (await getBufferContents(remote)) !== lines);
+
+ const updatedLines = await getBufferContents(remote);
+
+ // we now check that we have indeed formatted the code
+ expect(lines).not.toBe(updatedLines);
+ });
+};
+
+beforeAll(async () => {
+ server = new Server();
+ await server.listen(HOST, PORT);
+});
+
+afterAll(async () => {
+ await server.close();
+});
+
+// should ensure that we cache original fixture contents and
+// restore it on the afterEach
+beforeEach(async () => {
+ remote = new HeadlessRemoteClient({host: HOST, port: PORT});
+ await remote.connect(server);
+});
+
+afterEach(async () => {
+ if (remote.isConnected()) {
+ try {
+ const filename = await remote.call('expand', ['%:p']);
+
+ if (filename) {
+ // restore the file
+ await remote.execute('earlier 1d | noautocmd | write');
+ }
+ } catch (e) {
+ } finally {
+ await remote.close();
+ }
+ }
+});
+
+//test('PrettierVersion returns pluggin version', async () => {
+// const result = await remote.execute('PrettierVersion');
+// expect(result).toMatchSnapshot();
+//});
+
+// run formatting tests in all fixtures
+fs.readdirSync(FIXTURES_DIR).forEach(file => assertFormatting(file));