diff options
| author | Mitermayer Reis <mitermayer.reis@gmail.com> | 2020-02-05 11:08:03 +1100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-02-05 11:08:03 +1100 |
| commit | 49d91743b2df43f84edd199f877d494b4d8812f4 (patch) | |
| tree | cf856d77c9960a09eb3156937aa1b896b855bed6 /tests/formatting.test.js | |
| parent | 9eb448e45ef88e90681335fda32bcae52a09d6dc (diff) | |
| parent | b064c6ab82a3c57ea64360d762d661ad7e8ee54c (diff) | |
| download | vim-prettier-49d91743b2df43f84edd199f877d494b4d8812f4.tar.xz | |
Merge pull request #175 from prettier/release/1.x
Release/1.x
Diffstat (limited to 'tests/formatting.test.js')
| -rw-r--r-- | tests/formatting.test.js | 120 |
1 files changed, 120 insertions, 0 deletions
diff --git a/tests/formatting.test.js b/tests/formatting.test.js new file mode 100644 index 0000000..6514fec --- /dev/null +++ b/tests/formatting.test.js @@ -0,0 +1,120 @@ +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; + +jest.setTimeout(10000); + +const getBufferContents = async remote => + (await remote.call('getline', [1, '$'])).join('\n'); + +const sleep = ms => new Promise(resolve => setTimeout(resolve, ms)); +const waitUntil = (condition, timeout = 2000) => { + 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)); |
