api.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723
  1. var assert = require('assert'),
  2. fs = require('fs'),
  3. path = require('path'),
  4. read = fs.readFileSync,
  5. sass = process.env.NODESASS_COV ? require('../lib-cov') : require('../lib'),
  6. fixture = path.join.bind(null, __dirname, 'fixtures'),
  7. resolveFixture = path.resolve.bind(null, __dirname, 'fixtures');
  8. describe('api', function() {
  9. describe('.render(options)', function() {
  10. it('should compile sass to css with file', function(done) {
  11. var expected = read(fixture('simple/expected.css'), 'utf8').trim();
  12. sass.render({
  13. file: fixture('simple/index.scss'),
  14. success: function(result) {
  15. assert.equal(result.css.trim(), expected.replace(/\r\n/g, '\n'));
  16. done();
  17. }
  18. });
  19. });
  20. it('should compile sass to css with outFile set to absolute url', function(done) {
  21. sass.render({
  22. file: fixture('simple/index.scss'),
  23. sourceMap: true,
  24. outFile: fixture('simple/index-test.css'),
  25. success: function(result) {
  26. assert.equal(JSON.parse(result.map).file, 'index-test.css');
  27. done();
  28. }
  29. });
  30. });
  31. it('should compile sass to css with outFile set to relative url', function(done) {
  32. sass.render({
  33. file: fixture('simple/index.scss'),
  34. sourceMap: true,
  35. outFile: './index-test.css',
  36. success: function(result) {
  37. assert.equal(JSON.parse(result.map).file, 'index-test.css');
  38. done();
  39. }
  40. });
  41. });
  42. it('should compile sass to css with outFile and sourceMap set to relative url', function(done) {
  43. sass.render({
  44. file: fixture('simple/index.scss'),
  45. sourceMap: './deep/nested/index.map',
  46. outFile: './index-test.css',
  47. success: function(result) {
  48. assert.equal(JSON.parse(result.map).file, '../../index-test.css');
  49. done();
  50. }
  51. });
  52. });
  53. it('should compile sass to css with data', function(done) {
  54. var src = read(fixture('simple/index.scss'), 'utf8');
  55. var expected = read(fixture('simple/expected.css'), 'utf8').trim();
  56. sass.render({
  57. data: src,
  58. success: function(result) {
  59. assert.equal(result.css.trim(), expected.replace(/\r\n/g, '\n'));
  60. done();
  61. }
  62. });
  63. });
  64. it('should compile sass to css using indented syntax', function(done) {
  65. var src = read(fixture('indent/index.sass'), 'utf8');
  66. var expected = read(fixture('indent/expected.css'), 'utf8').trim();
  67. sass.render({
  68. data: src,
  69. indentedSyntax: true,
  70. success: function(result) {
  71. assert.equal(result.css.trim(), expected.replace(/\r\n/g, '\n'));
  72. done();
  73. }
  74. });
  75. });
  76. it('should throw error status 1 for bad input', function(done) {
  77. sass.render({
  78. data: '#navbar width 80%;',
  79. error: function(error) {
  80. assert(error.message);
  81. assert.equal(error.status, 1);
  82. done();
  83. }
  84. });
  85. });
  86. it('should compile with include paths', function(done) {
  87. var src = read(fixture('include-path/index.scss'), 'utf8');
  88. var expected = read(fixture('include-path/expected.css'), 'utf8').trim();
  89. sass.render({
  90. data: src,
  91. includePaths: [
  92. fixture('include-path/functions'),
  93. fixture('include-path/lib')
  94. ],
  95. success: function(result) {
  96. assert.equal(result.css.trim(), expected.replace(/\r\n/g, '\n'));
  97. done();
  98. }
  99. });
  100. });
  101. it('should compile with image path', function(done) {
  102. var src = read(fixture('image-path/index.scss'), 'utf8');
  103. var expected = read(fixture('image-path/expected.css'), 'utf8').trim();
  104. sass.render({
  105. data: src,
  106. imagePath: '/path/to/images',
  107. success: function(result) {
  108. assert.equal(result.css.trim(), expected.replace(/\r\n/g, '\n'));
  109. done();
  110. }
  111. });
  112. });
  113. it('should throw error with non-string image path', function(done) {
  114. var src = read(fixture('image-path/index.scss'), 'utf8');
  115. assert.throws(function() {
  116. sass.render({
  117. data: src,
  118. imagePath: ['/path/to/images']
  119. });
  120. });
  121. done();
  122. });
  123. it('should render with --precision option', function(done) {
  124. var src = read(fixture('precision/index.scss'), 'utf8');
  125. var expected = read(fixture('precision/expected.css'), 'utf8').trim();
  126. sass.render({
  127. data: src,
  128. precision: 10,
  129. success: function(result) {
  130. assert.equal(result.css.trim(), expected.replace(/\r\n/g, '\n'));
  131. done();
  132. }
  133. });
  134. });
  135. it('should contain all included files in stats when data is passed', function(done) {
  136. var src = read(fixture('include-files/index.scss'), 'utf8');
  137. var expected = [
  138. fixture('include-files/bar.scss').replace(/\\/g, '/'),
  139. fixture('include-files/foo.scss').replace(/\\/g, '/')
  140. ];
  141. sass.render({
  142. data: src,
  143. includePaths: [fixture('include-files')],
  144. success: function(result) {
  145. assert.deepEqual(result.stats.includedFiles, expected);
  146. done();
  147. }
  148. });
  149. });
  150. });
  151. describe('.render(importer)', function() {
  152. var src = read(fixture('include-files/index.scss'), 'utf8');
  153. it('should override imports with "data" as input and fires callback with file and contents', function(done) {
  154. sass.render({
  155. data: src,
  156. success: function(result) {
  157. assert.equal(result.css.trim(), 'div {\n color: yellow; }\n\ndiv {\n color: yellow; }');
  158. done();
  159. },
  160. importer: function(url, prev, done) {
  161. done({
  162. file: '/some/other/path.scss',
  163. contents: 'div {color: yellow;}'
  164. });
  165. }
  166. });
  167. });
  168. it('should override imports with "file" as input and fires callback with file and contents', function(done) {
  169. sass.render({
  170. file: fixture('include-files/index.scss'),
  171. success: function(result) {
  172. assert.equal(result.css.trim(), 'div {\n color: yellow; }\n\ndiv {\n color: yellow; }');
  173. done();
  174. },
  175. importer: function(url, prev, done) {
  176. done({
  177. file: '/some/other/path.scss',
  178. contents: 'div {color: yellow;}'
  179. });
  180. }
  181. });
  182. });
  183. it('should override imports with "data" as input and returns file and contents', function(done) {
  184. sass.render({
  185. data: src,
  186. success: function(result) {
  187. assert.equal(result.css.trim(), 'div {\n color: yellow; }\n\ndiv {\n color: yellow; }');
  188. done();
  189. },
  190. importer: function(url, prev) {
  191. return {
  192. file: prev + url,
  193. contents: 'div {color: yellow;}'
  194. };
  195. }
  196. });
  197. });
  198. it('should override imports with "file" as input and returns file and contents', function(done) {
  199. sass.render({
  200. file: fixture('include-files/index.scss'),
  201. success: function(result) {
  202. assert.equal(result.css.trim(), 'div {\n color: yellow; }\n\ndiv {\n color: yellow; }');
  203. done();
  204. },
  205. importer: function(url, prev) {
  206. return {
  207. file: prev + url,
  208. contents: 'div {color: yellow;}'
  209. };
  210. }
  211. });
  212. });
  213. it('should override imports with "data" as input and fires callback with file', function(done) {
  214. sass.render({
  215. data: src,
  216. success: function(result) {
  217. assert.equal(result.css.trim(), '');
  218. done();
  219. },
  220. importer: function(url, /* jshint unused:false */ prev, done) {
  221. done({
  222. file: path.resolve(path.dirname(fixture('include-files/index.scss')), url + (path.extname(url) ? '' : '.scss'))
  223. });
  224. }
  225. });
  226. });
  227. it('should override imports with "file" as input and fires callback with file', function(done) {
  228. sass.render({
  229. file: fixture('include-files/index.scss'),
  230. success: function(result) {
  231. assert.equal(result.css.trim(), '');
  232. done();
  233. },
  234. importer: function(url, prev, done) {
  235. done({
  236. file: path.resolve(path.dirname(prev), url + (path.extname(url) ? '' : '.scss'))
  237. });
  238. }
  239. });
  240. });
  241. it('should override imports with "data" as input and returns file', function(done) {
  242. sass.render({
  243. data: src,
  244. success: function(result) {
  245. assert.equal(result.css.trim(), '');
  246. done();
  247. },
  248. importer: function(url, /* jshint unused:false */ prev) {
  249. return {
  250. file: path.resolve(path.dirname(fixture('include-files/index.scss')), url + (path.extname(url) ? '' : '.scss'))
  251. };
  252. }
  253. });
  254. });
  255. it('should override imports with "file" as input and returns file', function(done) {
  256. sass.render({
  257. file: fixture('include-files/index.scss'),
  258. success: function(result) {
  259. assert.equal(result.css.trim(), '');
  260. done();
  261. },
  262. importer: function(url, prev) {
  263. return {
  264. file: path.resolve(path.dirname(prev), url + (path.extname(url) ? '' : '.scss'))
  265. };
  266. }
  267. });
  268. });
  269. it('should override imports with "data" as input and fires callback with contents', function(done) {
  270. sass.render({
  271. data: src,
  272. success: function(result) {
  273. assert.equal(result.css.trim(), 'div {\n color: yellow; }\n\ndiv {\n color: yellow; }');
  274. done();
  275. },
  276. importer: function(url, prev, done) {
  277. done({
  278. contents: 'div {color: yellow;}'
  279. });
  280. }
  281. });
  282. });
  283. it('should override imports with "file" as input and fires callback with contents', function(done) {
  284. sass.render({
  285. file: fixture('include-files/index.scss'),
  286. success: function(result) {
  287. assert.equal(result.css.trim(), 'div {\n color: yellow; }\n\ndiv {\n color: yellow; }');
  288. done();
  289. },
  290. importer: function(url, prev, done) {
  291. done({
  292. contents: 'div {color: yellow;}'
  293. });
  294. }
  295. });
  296. });
  297. it('should override imports with "data" as input and returns contents', function(done) {
  298. sass.render({
  299. data: src,
  300. success: function(result) {
  301. assert.equal(result.css.trim(), 'div {\n color: yellow; }\n\ndiv {\n color: yellow; }');
  302. done();
  303. },
  304. importer: function() {
  305. return {
  306. contents: 'div {color: yellow;}'
  307. };
  308. }
  309. });
  310. });
  311. it('should override imports with "file" as input and returns contents', function(done) {
  312. sass.render({
  313. file: fixture('include-files/index.scss'),
  314. success: function(result) {
  315. assert.equal(result.css.trim(), 'div {\n color: yellow; }\n\ndiv {\n color: yellow; }');
  316. done();
  317. },
  318. importer: function() {
  319. return {
  320. contents: 'div {color: yellow;}'
  321. };
  322. }
  323. });
  324. });
  325. });
  326. describe('.renderSync(options)', function() {
  327. it('should compile sass to css with file', function(done) {
  328. var expected = read(fixture('simple/expected.css'), 'utf8').trim();
  329. var result = sass.renderSync({file: fixture('simple/index.scss')});
  330. assert.equal(result.css.trim(), expected.replace(/\r\n/g, '\n'));
  331. done();
  332. });
  333. it('should compile sass to css with outFile set to absolute url', function(done) {
  334. var result = sass.renderSync({
  335. file: fixture('simple/index.scss'),
  336. sourceMap: true,
  337. outFile: fixture('simple/index-test.css')
  338. });
  339. assert.equal(JSON.parse(result.map).file, 'index-test.css');
  340. done();
  341. });
  342. it('should compile sass to css with outFile set to relative url', function(done) {
  343. var result = sass.renderSync({
  344. file: fixture('simple/index.scss'),
  345. sourceMap: true,
  346. outFile: './index-test.css'
  347. });
  348. assert.equal(JSON.parse(result.map).file, 'index-test.css');
  349. done();
  350. });
  351. it('should compile sass to css with outFile and sourceMap set to relative url', function(done) {
  352. var result = sass.renderSync({
  353. file: fixture('simple/index.scss'),
  354. sourceMap: './deep/nested/index.map',
  355. outFile: './index-test.css'
  356. });
  357. assert.equal(JSON.parse(result.map).file, '../../index-test.css');
  358. done();
  359. });
  360. it('should compile sass to css with data', function(done) {
  361. var src = read(fixture('simple/index.scss'), 'utf8');
  362. var expected = read(fixture('simple/expected.css'), 'utf8').trim();
  363. var result = sass.renderSync({data: src});
  364. assert.equal(result.css.trim(), expected.replace(/\r\n/g, '\n'));
  365. done();
  366. });
  367. it('should compile sass to css using indented syntax', function(done) {
  368. var src = read(fixture('indent/index.sass'), 'utf8');
  369. var expected = read(fixture('indent/expected.css'), 'utf8').trim();
  370. var css = sass.renderSync({
  371. data: src,
  372. indentedSyntax: true
  373. }).css.trim();
  374. assert.equal(css, expected.replace(/\r\n/g, '\n'));
  375. done();
  376. });
  377. it('should throw error for bad input', function(done) {
  378. assert.throws(function() {
  379. sass.renderSync({data: '#navbar width 80%;'});
  380. });
  381. done();
  382. });
  383. });
  384. describe('.renderSync(importer)', function() {
  385. var src = read(fixture('include-files/index.scss'), 'utf8');
  386. it('should override imports with "data" as input and returns file and contents', function(done) {
  387. var result = sass.renderSync({
  388. data: src,
  389. importer: function(url, prev) {
  390. return {
  391. file: prev + url,
  392. contents: 'div {color: yellow;}'
  393. };
  394. }
  395. });
  396. assert.equal(result.css.trim(), 'div {\n color: yellow; }\n\ndiv {\n color: yellow; }');
  397. done();
  398. });
  399. it('should override imports with "file" as input and returns file and contents', function(done) {
  400. var result = sass.renderSync({
  401. file: fixture('include-files/index.scss'),
  402. importer: function(url, prev) {
  403. return {
  404. file: prev + url,
  405. contents: 'div {color: yellow;}'
  406. };
  407. }
  408. });
  409. assert.equal(result.css.trim(), 'div {\n color: yellow; }\n\ndiv {\n color: yellow; }');
  410. done();
  411. });
  412. it('should override imports with "data" as input and returns file', function(done) {
  413. var result = sass.renderSync({
  414. data: src,
  415. importer: function(url, /* jshint unused:false */ prev) {
  416. return {
  417. file: path.resolve(path.dirname(fixture('include-files/index.scss')), url + (path.extname(url) ? '' : '.scss'))
  418. };
  419. }
  420. });
  421. assert.equal(result.css.trim(), '');
  422. done();
  423. });
  424. it('should override imports with "file" as input and returns file', function(done) {
  425. var result = sass.renderSync({
  426. file: fixture('include-files/index.scss'),
  427. importer: function(url, prev) {
  428. return {
  429. file: path.resolve(path.dirname(prev), url + (path.extname(url) ? '' : '.scss'))
  430. };
  431. }
  432. });
  433. assert.equal(result.css.trim(), '');
  434. done();
  435. });
  436. it('should override imports with "data" as input and returns contents', function(done) {
  437. var result = sass.renderSync({
  438. data: src,
  439. importer: function() {
  440. return {
  441. contents: 'div {color: yellow;}'
  442. };
  443. }
  444. });
  445. assert.equal(result.css.trim(), 'div {\n color: yellow; }\n\ndiv {\n color: yellow; }');
  446. done();
  447. });
  448. it('should override imports with "file" as input and returns contents', function(done) {
  449. var result = sass.renderSync({
  450. file: fixture('include-files/index.scss'),
  451. importer: function() {
  452. return {
  453. contents: 'div {color: yellow;}'
  454. };
  455. }
  456. });
  457. assert.equal(result.css.trim(), 'div {\n color: yellow; }\n\ndiv {\n color: yellow; }');
  458. done();
  459. });
  460. });
  461. describe('.render({stats: {}})', function() {
  462. var start = Date.now();
  463. it('should provide a start timestamp', function(done) {
  464. sass.render({
  465. file: fixture('include-files/index.scss'),
  466. success: function(result) {
  467. assert(typeof result.stats.start === 'number');
  468. assert(result.stats.start >= start);
  469. done();
  470. },
  471. error: function(err) {
  472. assert(!err);
  473. done();
  474. }
  475. });
  476. });
  477. it('should provide an end timestamp', function(done) {
  478. sass.render({
  479. file: fixture('include-files/index.scss'),
  480. success: function(result) {
  481. assert(typeof result.stats.end === 'number');
  482. assert(result.stats.end >= result.stats.start);
  483. done();
  484. },
  485. error: function(err) {
  486. assert(!err);
  487. done();
  488. }
  489. });
  490. });
  491. it('should provide a duration', function(done) {
  492. sass.render({
  493. file: fixture('include-files/index.scss'),
  494. success: function(result) {
  495. assert(typeof result.stats.duration === 'number');
  496. assert.equal(result.stats.end - result.stats.start, result.stats.duration);
  497. done();
  498. },
  499. error: function(err) {
  500. assert(!err);
  501. done();
  502. }
  503. });
  504. });
  505. it('should contain the given entry file', function(done) {
  506. sass.render({
  507. file: fixture('include-files/index.scss'),
  508. success: function(result) {
  509. assert.equal(result.stats.entry, fixture('include-files/index.scss'));
  510. done();
  511. },
  512. error: function(err) {
  513. assert(!err);
  514. done();
  515. }
  516. });
  517. });
  518. it('should contain an array of all included files', function(done) {
  519. var expected = [
  520. fixture('include-files/bar.scss').replace(/\\/g, '/'),
  521. fixture('include-files/foo.scss').replace(/\\/g, '/'),
  522. fixture('include-files/index.scss').replace(/\\/g, '/')
  523. ];
  524. sass.render({
  525. file: fixture('include-files/index.scss'),
  526. success: function(result) {
  527. assert.deepEqual(result.stats.includedFiles, expected);
  528. done();
  529. },
  530. error: function(err) {
  531. assert(!err);
  532. done();
  533. }
  534. });
  535. });
  536. it('should contain array with the entry if there are no import statements', function(done) {
  537. var expected = fixture('simple/index.scss').replace(/\\/g, '/');
  538. sass.render({
  539. file: fixture('simple/index.scss'),
  540. success: function(result) {
  541. assert.deepEqual(result.stats.includedFiles, [expected]);
  542. done();
  543. }
  544. });
  545. });
  546. it('should state `data` as entry file', function(done) {
  547. sass.render({
  548. data: read(fixture('simple/index.scss'), 'utf8'),
  549. success: function(result) {
  550. assert.equal(result.stats.entry, 'data');
  551. done();
  552. }
  553. });
  554. });
  555. it('should contain an empty array as includedFiles', function(done) {
  556. sass.render({
  557. data: read(fixture('simple/index.scss'), 'utf8'),
  558. success: function(result) {
  559. assert.deepEqual(result.stats.includedFiles, []);
  560. done();
  561. }
  562. });
  563. });
  564. });
  565. describe('.renderSync({stats: {}})', function() {
  566. var start = Date.now();
  567. var result = sass.renderSync({
  568. file: fixture('include-files/index.scss')
  569. });
  570. it('should provide a start timestamp', function(done) {
  571. assert(typeof result.stats.start === 'number');
  572. assert(result.stats.start >= start);
  573. done();
  574. });
  575. it('should provide an end timestamp', function(done) {
  576. assert(typeof result.stats.end === 'number');
  577. assert(result.stats.end >= result.stats.start);
  578. done();
  579. });
  580. it('should provide a duration', function(done) {
  581. assert(typeof result.stats.duration === 'number');
  582. assert.equal(result.stats.end - result.stats.start, result.stats.duration);
  583. done();
  584. });
  585. it('should contain the given entry file', function(done) {
  586. assert.equal(result.stats.entry, resolveFixture('include-files/index.scss'));
  587. done();
  588. });
  589. it('should contain an array of all included files', function(done) {
  590. var expected = [
  591. fixture('include-files/bar.scss').replace(/\\/g, '/'),
  592. fixture('include-files/foo.scss').replace(/\\/g, '/'),
  593. fixture('include-files/index.scss').replace(/\\/g, '/')
  594. ];
  595. assert.equal(result.stats.includedFiles[0], expected[0]);
  596. assert.equal(result.stats.includedFiles[1], expected[1]);
  597. assert.equal(result.stats.includedFiles[2], expected[2]);
  598. done();
  599. });
  600. it('should contain array with the entry if there are no import statements', function(done) {
  601. var expected = fixture('simple/index.scss').replace(/\\/g, '/');
  602. var result = sass.renderSync({
  603. file: fixture('simple/index.scss')
  604. });
  605. assert.deepEqual(result.stats.includedFiles, [expected]);
  606. done();
  607. });
  608. it('should state `data` as entry file', function(done) {
  609. var result = sass.renderSync({
  610. data: read(fixture('simple/index.scss'), 'utf8')
  611. });
  612. assert.equal(result.stats.entry, 'data');
  613. done();
  614. });
  615. it('should contain an empty array as includedFiles', function(done) {
  616. var result = sass.renderSync({
  617. data: read(fixture('simple/index.scss'), 'utf8')
  618. });
  619. assert.deepEqual(result.stats.includedFiles, []);
  620. done();
  621. });
  622. });
  623. describe('.info()', function() {
  624. it('should return a correct version info', function(done) {
  625. assert.equal(sass.info(), [
  626. 'node-sass version: ' + require('../package.json').version,
  627. 'libsass version: ' + require('../package.json').libsass
  628. ].join('\n'));
  629. done();
  630. });
  631. });
  632. });