Why-cant-i-assign-variables-using-p5-functions-and-variables-before-setup

hi!
i was setting up PhantomJS for headless browser testing with the existing code ,
i get following error saying

exact output

type or paste code hereRunning "connect:server" (connect) task
Started connect web server on http://localhost:8000

Running "mocha_phantomjs:all" (mocha_phantomjs) task
Did you just try to use p5.js's text() function? If so, you may want to move it into your sketch's setup() function.

For more details, see: https://github.com/processing/p5.js/wiki/Frequently-Asked-Questions#why-cant-i-assign-variables-using-p5-functions-and-variables-before-setup
TypeError: undefined is not an object (evaluating '(self.AudioContext||self.webkitAudioContext).prototype')

   at file:///home/endurance21/gsoc/p5.js-sound/lib/p5.sound.js:2978
   at file:///home/endurance21/gsoc/p5.js-sound/lib/p5.sound.js:2978
   in __webpack_require__ at file:///home/endurance21/gsoc/p5.js-sound/lib/p5.sound.js:84
   at file:///home/endurance21/gsoc/p5.js-sound/lib/p5.sound.js:2899
   at file:///home/endurance21/gsoc/p5.js-sound/lib/p5.sound.js:2972
   in __webpack_require__ at file:///home/endurance21/gsoc/p5.js-sound/lib/p5.sound.js:84
   at file:///home/endurance21/gsoc/p5.js-sound/lib/p5.sound.js:120

i didn’t understand the part saying

why-cant-i-assign-variables-using-p5-functions-and-variables-before-setup
which i think is something to do p5.js code

@lmccart @therewasaguy
please help!

1 Like

Might want to paste the rest of the code

@paulgoux

this is what i did in gruntfile.js

  connect: {
      server: {
        options: {
          port: 8000,
          base: '.',
        }
      }
    },

    mocha_phantomjs: {
        all: ['test/*.html']
    }
  });

here is the index.html in test folder that is to rendered on headless browser

<html>
<head>
	<meta charset="utf-8">
	<title>TESTS</title>
	<script src="../lib/p5.js"></script>
	<script src="../lib/p5.sound.js"></script>
	<script>
		window.setup = function setup() {};
		new p5();
	</script>
	<script src="./testDeps/mocha.js"></script>
	<script data-main="test.js" src="./testDeps/require.js"></script>
	<link rel="stylesheet" href="./testDeps/mocha.css" />
</head>
<body>
	<div id="mocha"></div>
	<script>
		mocha.setup('bdd');
	</script>
</body>
</html>

dependecies installed for using headless browser id mocha-phantomjs

please see if it enough to know have a idea about the issue!

1 Like

Just looking at the error would suggest that there is an issue with one of your variables. P5.js can assign variables before setup, however they do have limitations such as cos and sin can only be used in setup and draw, so if paired with variables called or created outside it might cause a problem. However your issue reminds me of when i declare an object variable without initialisation which then causes a problem later as the program cannot understand it, so i set the object to null.

1 Like

@paulgoux thanks for your help !

1 Like