版博士V2.0程序
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

68 lines
1.7 KiB

  1. const $ = require('../index');
  2. const fs = require('fs');
  3. const config = require('./config');
  4. const CODE = `
  5. function test(){
  6. let a = 1;
  7. let b = 2;
  8. }
  9. test();
  10. `
  11. const HTML_CODE = `
  12. <script type="text/javascript" src="./test.js"></script>
  13. <div>test</div>
  14. `
  15. const ERROR_CODE = `function(){ console.log('error')`;
  16. const PATH = './test.js';
  17. const HTML_PATH = './test.html';
  18. const ERROR_CODE_PATH = './error.js';
  19. beforeEach(() => {
  20. fs.writeFileSync(PATH, CODE);
  21. fs.writeFileSync(HTML_PATH, HTML_CODE);
  22. fs.writeFileSync(ERROR_CODE_PATH, ERROR_CODE);
  23. });
  24. afterEach(() => {
  25. fs.unlinkSync(PATH);
  26. fs.unlinkSync(HTML_PATH);
  27. fs.unlinkSync(ERROR_CODE_PATH);
  28. });
  29. test('$.loadFile: js code should not throw error', () => {
  30. expect(() => {
  31. $.loadFile(PATH);
  32. }).not.toThrow();
  33. })
  34. test('$.loadFile: js code should be same', () => {
  35. const G = $.loadFile(PATH);
  36. const newG = $(CODE);
  37. expect(G.generate()).toEqual(newG.generate());
  38. })
  39. test('$.loadFile: non-existent file should throw error', () => {
  40. expect(() => {
  41. const G = $.loadFile('./non-existent.js');
  42. }).toThrow();
  43. })
  44. test('$.loadFile: error code file should throw error', () => {
  45. const G = $.loadFile(ERROR_CODE_PATH);
  46. expect(G.error).toEqual('Only correct js / html / vue could be parse successfully, please check the code or parseOptions!');
  47. })
  48. test('$.loadFile: html code should not throw error', () => {
  49. expect(() => {
  50. $.loadFile(HTML_PATH, config.html);
  51. }).not.toThrow();
  52. })
  53. test('$.loadFile: html code should be same', () => {
  54. const G = $.loadFile(HTML_PATH, config.html);
  55. const newG = $(HTML_CODE, config.html);
  56. expect(G.node).toEqual(newG.node);
  57. })