remark-growi-plugin.test.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /**
  2. * @typedef {import('mdast').Root} Root
  3. */
  4. import fs from 'node:fs';
  5. import path from 'node:path';
  6. import { isHidden } from 'is-hidden';
  7. import { remark } from 'remark';
  8. import test from 'tape';
  9. import { readSync } from 'to-vfile';
  10. import { unified } from 'unified';
  11. import { remarkGrowiPlugin } from '../src/remark-growi-plugin.js';
  12. test('directive()', (t) => {
  13. t.doesNotThrow(() => {
  14. remark().use(remarkGrowiPlugin).freeze();
  15. }, 'should not throw if not passed options');
  16. t.doesNotThrow(() => {
  17. unified().use(remarkGrowiPlugin).freeze();
  18. }, 'should not throw if without parser or compiler');
  19. t.end();
  20. });
  21. test('fixtures', (t) => {
  22. const base = path.join('test', 'fixtures');
  23. const entries = fs.readdirSync(base).filter(d => !isHidden(d));
  24. t.plan(entries.length);
  25. let index = -1;
  26. while (++index < entries.length) {
  27. const fixture = entries[index];
  28. t.test(fixture, (st) => {
  29. const file = readSync(path.join(base, fixture, 'input.md'));
  30. const input = String(file);
  31. const outputPath = path.join(base, fixture, 'output.md');
  32. const treePath = path.join(base, fixture, 'tree.json');
  33. const proc = remark().use(remarkGrowiPlugin).freeze();
  34. const actual = proc.parse(file);
  35. /** @type {string} */
  36. let output;
  37. /** @type {Root} */
  38. let expected;
  39. try {
  40. expected = JSON.parse(String(fs.readFileSync(treePath)));
  41. }
  42. catch {
  43. // New fixture.
  44. fs.writeFileSync(treePath, `${JSON.stringify(actual, null, 2)}\n`);
  45. expected = actual;
  46. }
  47. try {
  48. output = fs.readFileSync(outputPath, 'utf8');
  49. }
  50. catch {
  51. output = input;
  52. }
  53. st.deepEqual(actual, expected, 'tree');
  54. st.equal(String(proc.processSync(file)), output, 'process');
  55. st.end();
  56. });
  57. }
  58. });